1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2007-2012 Nicira, Inc. 4 */ 5 6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 7 8 #include <linux/if_arp.h> 9 #include <linux/if_bridge.h> 10 #include <linux/if_vlan.h> 11 #include <linux/kernel.h> 12 #include <linux/rtnetlink.h> 13 #include <linux/skbuff.h> 14 #include <linux/openvswitch.h> 15 #include <linux/export.h> 16 17 #include <net/ip_tunnels.h> 18 #include <net/rtnetlink.h> 19 20 #include "datapath.h" 21 #include "vport.h" 22 #include "vport-internal_dev.h" 23 #include "vport-netdev.h" 24 25 static struct vport_ops ovs_netdev_vport_ops; 26 27 /* Must be called with rcu_read_lock. */ 28 static void netdev_port_receive(struct sk_buff *skb) 29 { 30 struct vport *vport; 31 32 vport = ovs_netdev_get_vport(skb->dev); 33 if (unlikely(!vport)) 34 goto error; 35 36 if (unlikely(skb_warn_if_lro(skb))) 37 goto error; 38 39 /* Make our own copy of the packet. Otherwise we will mangle the 40 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET). 41 */ 42 skb = skb_share_check(skb, GFP_ATOMIC); 43 if (unlikely(!skb)) 44 return; 45 46 if (skb->dev->type == ARPHRD_ETHER) 47 skb_push_rcsum(skb, ETH_HLEN); 48 49 ovs_vport_receive(vport, skb, skb_tunnel_info(skb)); 50 return; 51 error: 52 kfree_skb(skb); 53 } 54 55 /* Called with rcu_read_lock and bottom-halves disabled. */ 56 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb) 57 { 58 struct sk_buff *skb = *pskb; 59 60 if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) 61 return RX_HANDLER_PASS; 62 63 netdev_port_receive(skb); 64 return RX_HANDLER_CONSUMED; 65 } 66 67 static struct net_device *get_dpdev(const struct datapath *dp) 68 { 69 struct vport *local; 70 71 local = ovs_vport_ovsl(dp, OVSP_LOCAL); 72 return local->dev; 73 } 74 75 static struct vport *ovs_netdev_link(struct vport *vport) 76 { 77 int err; 78 79 if (WARN_ON_ONCE(!vport->dev)) { 80 err = -ENODEV; 81 goto error_free_vport; 82 } 83 84 rtnl_lock(); 85 /* Do not link devices that are not registered to avoid a potential 86 * race with the NETDEV_UNREGISTER notification in dp_device_event(). 87 */ 88 if (vport->dev->reg_state != NETREG_REGISTERED) { 89 err = -ENODEV; 90 goto error_put_unlock; 91 } 92 93 err = netdev_master_upper_dev_link(vport->dev, 94 get_dpdev(vport->dp), 95 NULL, NULL, NULL); 96 if (err) 97 goto error_put_unlock; 98 99 err = netdev_rx_handler_register(vport->dev, netdev_frame_hook, 100 vport); 101 if (err) 102 goto error_master_upper_dev_unlink; 103 104 dev_disable_lro(vport->dev); 105 dev_set_promiscuity(vport->dev, 1); 106 vport->dev->priv_flags |= IFF_OVS_DATAPATH; 107 rtnl_unlock(); 108 109 return vport; 110 111 error_master_upper_dev_unlink: 112 netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp)); 113 error_put_unlock: 114 netdev_put(vport->dev, &vport->dev_tracker); 115 rtnl_unlock(); 116 error_free_vport: 117 ovs_vport_free(vport); 118 return ERR_PTR(err); 119 } 120 121 static struct vport *netdev_create(const struct vport_parms *parms) 122 { 123 struct vport *vport; 124 int err; 125 126 vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms); 127 if (IS_ERR(vport)) 128 return vport; 129 130 vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), parms->name); 131 if (!vport->dev) { 132 err = -ENODEV; 133 goto error_free_vport; 134 } 135 netdev_tracker_alloc(vport->dev, &vport->dev_tracker, GFP_KERNEL); 136 137 /* Ensure that the provided name is not an alias. */ 138 if (strcmp(parms->name, ovs_vport_name(vport))) { 139 err = -ENODEV; 140 goto error_put; 141 } 142 143 if (vport->dev->flags & IFF_LOOPBACK || 144 (vport->dev->type != ARPHRD_ETHER && 145 vport->dev->type != ARPHRD_NONE) || 146 ovs_is_internal_dev(vport->dev)) { 147 err = -EINVAL; 148 goto error_put; 149 } 150 151 return ovs_netdev_link(vport); 152 error_put: 153 netdev_put(vport->dev, &vport->dev_tracker); 154 error_free_vport: 155 ovs_vport_free(vport); 156 return ERR_PTR(err); 157 } 158 159 static void vport_netdev_free(struct rcu_head *rcu) 160 { 161 struct vport *vport = container_of(rcu, struct vport, rcu); 162 163 netdev_put(vport->dev, &vport->dev_tracker); 164 ovs_vport_free(vport); 165 } 166 167 void ovs_netdev_detach_dev(struct vport *vport) 168 { 169 ASSERT_RTNL(); 170 netdev_rx_handler_unregister(vport->dev); 171 netdev_upper_dev_unlink(vport->dev, 172 netdev_master_upper_dev_get(vport->dev)); 173 dev_set_promiscuity(vport->dev, -1); 174 175 /* paired with smp_mb() in netdev_destroy() */ 176 smp_wmb(); 177 178 vport->dev->priv_flags &= ~IFF_OVS_DATAPATH; 179 } 180 181 static void netdev_destroy(struct vport *vport) 182 { 183 /* When called from ovs_db_notify_wq() after a dp_device_event(), the 184 * port has already been detached, so we can avoid taking the RTNL by 185 * checking this first. 186 */ 187 if (netif_is_ovs_port(vport->dev)) { 188 rtnl_lock(); 189 /* Check again while holding the lock to ensure we don't race 190 * with the netdev notifier and detach twice. 191 */ 192 if (netif_is_ovs_port(vport->dev)) 193 ovs_netdev_detach_dev(vport); 194 rtnl_unlock(); 195 } 196 197 /* paired with smp_wmb() in ovs_netdev_detach_dev() */ 198 smp_mb(); 199 200 call_rcu(&vport->rcu, vport_netdev_free); 201 } 202 203 /* Returns null if this device is not attached to a datapath. */ 204 struct vport *ovs_netdev_get_vport(struct net_device *dev) 205 { 206 if (likely(netif_is_ovs_port(dev))) 207 return (struct vport *) 208 rcu_dereference_rtnl(dev->rx_handler_data); 209 else 210 return NULL; 211 } 212 213 static struct vport_ops ovs_netdev_vport_ops = { 214 .type = OVS_VPORT_TYPE_NETDEV, 215 .create = netdev_create, 216 .destroy = netdev_destroy, 217 .send = dev_queue_xmit, 218 }; 219 220 int __init ovs_netdev_init(void) 221 { 222 return ovs_vport_ops_register(&ovs_netdev_vport_ops); 223 } 224 225 void ovs_netdev_exit(void) 226 { 227 ovs_vport_ops_unregister(&ovs_netdev_vport_ops); 228 } 229