blob: cecf8c63096cd602f4c740431152ed56964a2495 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Sainath Grandhia8e04692017-02-10 16:03:46 -08002#include <linux/etherdevice.h>
3#include <linux/if_macvlan.h>
Sainath Grandhi635b8c82017-02-10 16:03:47 -08004#include <linux/if_tap.h>
Sainath Grandhia8e04692017-02-10 16:03:46 -08005#include <linux/if_vlan.h>
6#include <linux/interrupt.h>
7#include <linux/nsproxy.h>
8#include <linux/compat.h>
9#include <linux/if_tun.h>
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/cache.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010013#include <linux/sched/signal.h>
Sainath Grandhia8e04692017-02-10 16:03:46 -080014#include <linux/types.h>
15#include <linux/slab.h>
16#include <linux/wait.h>
17#include <linux/cdev.h>
18#include <linux/idr.h>
19#include <linux/fs.h>
20#include <linux/uio.h>
21
22#include <net/net_namespace.h>
23#include <net/rtnetlink.h>
24#include <net/sock.h>
25#include <linux/virtio_net.h>
26#include <linux/skb_array.h>
27
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -080028struct macvtap_dev {
29 struct macvlan_dev vlan;
30 struct tap_dev tap;
31};
32
Sainath Grandhia8e04692017-02-10 16:03:46 -080033/*
34 * Variables for dealing with macvtaps device numbers.
35 */
36static dev_t macvtap_major;
Sainath Grandhia8e04692017-02-10 16:03:46 -080037
38static const void *macvtap_net_namespace(struct device *d)
39{
40 struct net_device *dev = to_net_dev(d->parent);
41 return dev_net(dev);
42}
43
44static struct class macvtap_class = {
45 .name = "macvtap",
46 .owner = THIS_MODULE,
47 .ns_type = &net_ns_type_operations,
48 .namespace = macvtap_net_namespace,
49};
50static struct cdev macvtap_cdev;
51
52#define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
David S. Millerfb652fd2017-07-03 06:33:08 -070053 NETIF_F_TSO6)
Sainath Grandhia8e04692017-02-10 16:03:46 -080054
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -080055static void macvtap_count_tx_dropped(struct tap_dev *tap)
56{
57 struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
58 struct macvlan_dev *vlan = &vlantap->vlan;
59
60 this_cpu_inc(vlan->pcpu_stats->tx_dropped);
61}
62
63static void macvtap_count_rx_dropped(struct tap_dev *tap)
64{
65 struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
66 struct macvlan_dev *vlan = &vlantap->vlan;
67
68 macvlan_count_rx(vlan, 0, 0, 0);
69}
70
71static void macvtap_update_features(struct tap_dev *tap,
72 netdev_features_t features)
73{
74 struct macvtap_dev *vlantap = container_of(tap, struct macvtap_dev, tap);
75 struct macvlan_dev *vlan = &vlantap->vlan;
76
77 vlan->set_features = features;
78 netdev_update_features(vlan->dev);
79}
80
Matthias Schiffer7a3f4a12017-06-25 23:55:59 +020081static int macvtap_newlink(struct net *src_net, struct net_device *dev,
82 struct nlattr *tb[], struct nlattr *data[],
83 struct netlink_ext_ack *extack)
Sainath Grandhia8e04692017-02-10 16:03:46 -080084{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -080085 struct macvtap_dev *vlantap = netdev_priv(dev);
Sainath Grandhia8e04692017-02-10 16:03:46 -080086 int err;
87
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -080088 INIT_LIST_HEAD(&vlantap->tap.queue_list);
Sainath Grandhia8e04692017-02-10 16:03:46 -080089
90 /* Since macvlan supports all offloads by default, make
91 * tap support all offloads also.
92 */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -080093 vlantap->tap.tap_features = TUN_OFFLOADS;
Sainath Grandhia8e04692017-02-10 16:03:46 -080094
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -080095 /* Register callbacks for rx/tx drops accounting and updating
96 * net_device features
97 */
98 vlantap->tap.count_tx_dropped = macvtap_count_tx_dropped;
99 vlantap->tap.count_rx_dropped = macvtap_count_rx_dropped;
100 vlantap->tap.update_features = macvtap_update_features;
101
102 err = netdev_rx_handler_register(dev, tap_handle_frame, &vlantap->tap);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800103 if (err)
104 return err;
105
106 /* Don't put anything that may fail after macvlan_common_newlink
107 * because we can't undo what it does.
108 */
David Ahern42ab19e2017-10-04 17:48:47 -0700109 err = macvlan_common_newlink(src_net, dev, tb, data, extack);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800110 if (err) {
111 netdev_rx_handler_unregister(dev);
112 return err;
113 }
114
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800115 vlantap->tap.dev = vlantap->vlan.dev;
116
Sainath Grandhia8e04692017-02-10 16:03:46 -0800117 return 0;
118}
119
120static void macvtap_dellink(struct net_device *dev,
121 struct list_head *head)
122{
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800123 struct macvtap_dev *vlantap = netdev_priv(dev);
124
Sainath Grandhia8e04692017-02-10 16:03:46 -0800125 netdev_rx_handler_unregister(dev);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800126 tap_del_queues(&vlantap->tap);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800127 macvlan_dellink(dev, head);
128}
129
130static void macvtap_setup(struct net_device *dev)
131{
132 macvlan_common_setup(dev);
133 dev->tx_queue_len = TUN_READQ_SIZE;
134}
135
Sven Eckelmanna0219212022-02-28 01:32:40 +0100136static struct net *macvtap_link_net(const struct net_device *dev)
137{
138 return dev_net(macvlan_dev_real_dev(dev));
139}
140
Sainath Grandhia8e04692017-02-10 16:03:46 -0800141static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
142 .kind = "macvtap",
143 .setup = macvtap_setup,
144 .newlink = macvtap_newlink,
145 .dellink = macvtap_dellink,
Sven Eckelmanna0219212022-02-28 01:32:40 +0100146 .get_link_net = macvtap_link_net,
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800147 .priv_size = sizeof(struct macvtap_dev),
Sainath Grandhia8e04692017-02-10 16:03:46 -0800148};
149
150static int macvtap_device_event(struct notifier_block *unused,
151 unsigned long event, void *ptr)
152{
153 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800154 struct macvtap_dev *vlantap;
Sainath Grandhia8e04692017-02-10 16:03:46 -0800155 struct device *classdev;
156 dev_t devt;
157 int err;
158 char tap_name[IFNAMSIZ];
159
160 if (dev->rtnl_link_ops != &macvtap_link_ops)
161 return NOTIFY_DONE;
162
163 snprintf(tap_name, IFNAMSIZ, "tap%d", dev->ifindex);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800164 vlantap = netdev_priv(dev);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800165
166 switch (event) {
167 case NETDEV_REGISTER:
168 /* Create the device node here after the network device has
169 * been registered but before register_netdevice has
170 * finished running.
171 */
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800172 err = tap_get_minor(macvtap_major, &vlantap->tap);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800173 if (err)
174 return notifier_from_errno(err);
175
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800176 devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800177 classdev = device_create(&macvtap_class, &dev->dev, devt,
Jean Sacren1c5b5b32021-10-15 23:41:34 -0600178 dev, "%s", tap_name);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800179 if (IS_ERR(classdev)) {
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800180 tap_free_minor(macvtap_major, &vlantap->tap);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800181 return notifier_from_errno(PTR_ERR(classdev));
182 }
183 err = sysfs_create_link(&dev->dev.kobj, &classdev->kobj,
184 tap_name);
185 if (err)
186 return notifier_from_errno(err);
187 break;
188 case NETDEV_UNREGISTER:
189 /* vlan->minor == 0 if NETDEV_REGISTER above failed */
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800190 if (vlantap->tap.minor == 0)
Sainath Grandhia8e04692017-02-10 16:03:46 -0800191 break;
192 sysfs_remove_link(&dev->dev.kobj, tap_name);
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800193 devt = MKDEV(MAJOR(macvtap_major), vlantap->tap.minor);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800194 device_destroy(&macvtap_class, devt);
Sainath Grandhid9f1f612017-02-10 16:03:50 -0800195 tap_free_minor(macvtap_major, &vlantap->tap);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800196 break;
197 case NETDEV_CHANGE_TX_QUEUE_LEN:
Sainath Grandhi6fe3faf2017-02-10 16:03:49 -0800198 if (tap_queue_resize(&vlantap->tap))
Sainath Grandhia8e04692017-02-10 16:03:46 -0800199 return NOTIFY_BAD;
200 break;
201 }
202
203 return NOTIFY_DONE;
204}
205
206static struct notifier_block macvtap_notifier_block __read_mostly = {
207 .notifier_call = macvtap_device_event,
208};
209
Sainath Grandhia8e04692017-02-10 16:03:46 -0800210static int macvtap_init(void)
211{
212 int err;
213
Girish Moodalbaildea6e192017-10-27 00:00:16 -0700214 err = tap_create_cdev(&macvtap_cdev, &macvtap_major, "macvtap",
215 THIS_MODULE);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800216 if (err)
217 goto out1;
218
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800219 err = class_register(&macvtap_class);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800220 if (err)
221 goto out2;
222
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800223 err = register_netdevice_notifier(&macvtap_notifier_block);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800224 if (err)
225 goto out3;
226
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800227 err = macvlan_link_register(&macvtap_link_ops);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800228 if (err)
229 goto out4;
230
Sainath Grandhia8e04692017-02-10 16:03:46 -0800231 return 0;
232
Sainath Grandhia8e04692017-02-10 16:03:46 -0800233out4:
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800234 unregister_netdevice_notifier(&macvtap_notifier_block);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800235out3:
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800236 class_unregister(&macvtap_class);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800237out2:
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800238 tap_destroy_cdev(macvtap_major, &macvtap_cdev);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800239out1:
240 return err;
241}
242module_init(macvtap_init);
243
Sainath Grandhia8e04692017-02-10 16:03:46 -0800244static void macvtap_exit(void)
245{
246 rtnl_link_unregister(&macvtap_link_ops);
247 unregister_netdevice_notifier(&macvtap_notifier_block);
248 class_unregister(&macvtap_class);
Sainath Grandhiebc05ba2017-02-10 16:03:48 -0800249 tap_destroy_cdev(macvtap_major, &macvtap_cdev);
Sainath Grandhia8e04692017-02-10 16:03:46 -0800250}
251module_exit(macvtap_exit);
252
253MODULE_ALIAS_RTNL_LINK("macvtap");
254MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
255MODULE_LICENSE("GPL");