1ccb1352eSJesse Gross /* 2caf2ee14SRaju Subramanian * Copyright (c) 2007-2012 Nicira, Inc. 3ccb1352eSJesse Gross * 4ccb1352eSJesse Gross * This program is free software; you can redistribute it and/or 5ccb1352eSJesse Gross * modify it under the terms of version 2 of the GNU General Public 6ccb1352eSJesse Gross * License as published by the Free Software Foundation. 7ccb1352eSJesse Gross * 8ccb1352eSJesse Gross * This program is distributed in the hope that it will be useful, but 9ccb1352eSJesse Gross * WITHOUT ANY WARRANTY; without even the implied warranty of 10ccb1352eSJesse Gross * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11ccb1352eSJesse Gross * General Public License for more details. 12ccb1352eSJesse Gross * 13ccb1352eSJesse Gross * You should have received a copy of the GNU General Public License 14ccb1352eSJesse Gross * along with this program; if not, write to the Free Software 15ccb1352eSJesse Gross * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 16ccb1352eSJesse Gross * 02110-1301, USA 17ccb1352eSJesse Gross */ 18ccb1352eSJesse Gross 19ccb1352eSJesse Gross #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20ccb1352eSJesse Gross 21ccb1352eSJesse Gross #include <linux/if_arp.h> 22ccb1352eSJesse Gross #include <linux/if_bridge.h> 23ccb1352eSJesse Gross #include <linux/if_vlan.h> 24ccb1352eSJesse Gross #include <linux/kernel.h> 25ccb1352eSJesse Gross #include <linux/llc.h> 26ccb1352eSJesse Gross #include <linux/rtnetlink.h> 27ccb1352eSJesse Gross #include <linux/skbuff.h> 282537b4ddSJiri Pirko #include <linux/openvswitch.h> 29ccb1352eSJesse Gross 30ccb1352eSJesse Gross #include <net/llc.h> 31ccb1352eSJesse Gross 32ccb1352eSJesse Gross #include "datapath.h" 33ccb1352eSJesse Gross #include "vport-internal_dev.h" 34ccb1352eSJesse Gross #include "vport-netdev.h" 35ccb1352eSJesse Gross 3662b9c8d0SThomas Graf static struct vport_ops ovs_netdev_vport_ops; 3762b9c8d0SThomas Graf 38ccb1352eSJesse Gross /* Must be called with rcu_read_lock. */ 39ccb1352eSJesse Gross static void netdev_port_receive(struct vport *vport, struct sk_buff *skb) 40ccb1352eSJesse Gross { 41d9d59089SJesse Gross if (unlikely(!vport)) 42d9d59089SJesse Gross goto error; 43d9d59089SJesse Gross 44d9d59089SJesse Gross if (unlikely(skb_warn_if_lro(skb))) 45d9d59089SJesse Gross goto error; 46ccb1352eSJesse Gross 47ccb1352eSJesse Gross /* Make our own copy of the packet. Otherwise we will mangle the 48ccb1352eSJesse Gross * packet for anyone who came before us (e.g. tcpdump via AF_PACKET). 49d176ca2aSCong Wang */ 50ccb1352eSJesse Gross skb = skb_share_check(skb, GFP_ATOMIC); 51ccb1352eSJesse Gross if (unlikely(!skb)) 52ccb1352eSJesse Gross return; 53ccb1352eSJesse Gross 54ccb1352eSJesse Gross skb_push(skb, ETH_HLEN); 55b34df5e8SPravin B Shelar ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN); 56b34df5e8SPravin B Shelar 577d5437c7SPravin B Shelar ovs_vport_receive(vport, skb, NULL); 58d9d59089SJesse Gross return; 59d9d59089SJesse Gross 60d9d59089SJesse Gross error: 61d9d59089SJesse Gross kfree_skb(skb); 62ccb1352eSJesse Gross } 63ccb1352eSJesse Gross 64ccb1352eSJesse Gross /* Called with rcu_read_lock and bottom-halves disabled. */ 65ccb1352eSJesse Gross static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb) 66ccb1352eSJesse Gross { 67ccb1352eSJesse Gross struct sk_buff *skb = *pskb; 68ccb1352eSJesse Gross struct vport *vport; 69ccb1352eSJesse Gross 70ccb1352eSJesse Gross if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) 71ccb1352eSJesse Gross return RX_HANDLER_PASS; 72ccb1352eSJesse Gross 73ccb1352eSJesse Gross vport = ovs_netdev_get_vport(skb->dev); 74ccb1352eSJesse Gross 75ccb1352eSJesse Gross netdev_port_receive(vport, skb); 76ccb1352eSJesse Gross 77ccb1352eSJesse Gross return RX_HANDLER_CONSUMED; 78ccb1352eSJesse Gross } 79ccb1352eSJesse Gross 8012eb18f7SThomas Graf static struct net_device *get_dpdev(const struct datapath *dp) 812537b4ddSJiri Pirko { 822537b4ddSJiri Pirko struct vport *local; 832537b4ddSJiri Pirko 842537b4ddSJiri Pirko local = ovs_vport_ovsl(dp, OVSP_LOCAL); 852537b4ddSJiri Pirko BUG_ON(!local); 86be4ace6eSThomas Graf return local->dev; 872537b4ddSJiri Pirko } 882537b4ddSJiri Pirko 89be4ace6eSThomas Graf static struct vport *netdev_link(struct vport *vport, const char *name) 90ccb1352eSJesse Gross { 91ccb1352eSJesse Gross int err; 92ccb1352eSJesse Gross 93be4ace6eSThomas Graf vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name); 94be4ace6eSThomas Graf if (!vport->dev) { 95ccb1352eSJesse Gross err = -ENODEV; 96ccb1352eSJesse Gross goto error_free_vport; 97ccb1352eSJesse Gross } 98ccb1352eSJesse Gross 99be4ace6eSThomas Graf if (vport->dev->flags & IFF_LOOPBACK || 100be4ace6eSThomas Graf vport->dev->type != ARPHRD_ETHER || 101be4ace6eSThomas Graf ovs_is_internal_dev(vport->dev)) { 102ccb1352eSJesse Gross err = -EINVAL; 103ccb1352eSJesse Gross goto error_put; 104ccb1352eSJesse Gross } 105ccb1352eSJesse Gross 1068e4e1713SPravin B Shelar rtnl_lock(); 107be4ace6eSThomas Graf err = netdev_master_upper_dev_link(vport->dev, 1082537b4ddSJiri Pirko get_dpdev(vport->dp)); 1092537b4ddSJiri Pirko if (err) 1102537b4ddSJiri Pirko goto error_unlock; 1112537b4ddSJiri Pirko 112be4ace6eSThomas Graf err = netdev_rx_handler_register(vport->dev, netdev_frame_hook, 113ccb1352eSJesse Gross vport); 114ccb1352eSJesse Gross if (err) 1152537b4ddSJiri Pirko goto error_master_upper_dev_unlink; 116ccb1352eSJesse Gross 117be4ace6eSThomas Graf dev_disable_lro(vport->dev); 118be4ace6eSThomas Graf dev_set_promiscuity(vport->dev, 1); 119be4ace6eSThomas Graf vport->dev->priv_flags |= IFF_OVS_DATAPATH; 1208e4e1713SPravin B Shelar rtnl_unlock(); 121ccb1352eSJesse Gross 122ccb1352eSJesse Gross return vport; 123ccb1352eSJesse Gross 1242537b4ddSJiri Pirko error_master_upper_dev_unlink: 125be4ace6eSThomas Graf netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp)); 1268e4e1713SPravin B Shelar error_unlock: 1278e4e1713SPravin B Shelar rtnl_unlock(); 128ccb1352eSJesse Gross error_put: 129be4ace6eSThomas Graf dev_put(vport->dev); 130ccb1352eSJesse Gross error_free_vport: 131ccb1352eSJesse Gross ovs_vport_free(vport); 132ccb1352eSJesse Gross return ERR_PTR(err); 133ccb1352eSJesse Gross } 134ccb1352eSJesse Gross 135be4ace6eSThomas Graf static struct vport *netdev_create(const struct vport_parms *parms) 136be4ace6eSThomas Graf { 137be4ace6eSThomas Graf struct vport *vport; 138be4ace6eSThomas Graf 139be4ace6eSThomas Graf vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms); 140be4ace6eSThomas Graf if (IS_ERR(vport)) 141be4ace6eSThomas Graf return vport; 142be4ace6eSThomas Graf 143be4ace6eSThomas Graf return netdev_link(vport, parms->name); 144be4ace6eSThomas Graf } 145be4ace6eSThomas Graf 14692eb1d47SJesse Gross static void free_port_rcu(struct rcu_head *rcu) 14792eb1d47SJesse Gross { 148be4ace6eSThomas Graf struct vport *vport = container_of(rcu, struct vport, rcu); 14992eb1d47SJesse Gross 150be4ace6eSThomas Graf dev_put(vport->dev); 151be4ace6eSThomas Graf ovs_vport_free(vport); 15292eb1d47SJesse Gross } 15392eb1d47SJesse Gross 154b07c2651SAlexei Starovoitov void ovs_netdev_detach_dev(struct vport *vport) 155b07c2651SAlexei Starovoitov { 156b07c2651SAlexei Starovoitov ASSERT_RTNL(); 157be4ace6eSThomas Graf vport->dev->priv_flags &= ~IFF_OVS_DATAPATH; 158be4ace6eSThomas Graf netdev_rx_handler_unregister(vport->dev); 159be4ace6eSThomas Graf netdev_upper_dev_unlink(vport->dev, 160be4ace6eSThomas Graf netdev_master_upper_dev_get(vport->dev)); 161be4ace6eSThomas Graf dev_set_promiscuity(vport->dev, -1); 162b07c2651SAlexei Starovoitov } 163b07c2651SAlexei Starovoitov 164ccb1352eSJesse Gross static void netdev_destroy(struct vport *vport) 165ccb1352eSJesse Gross { 1668e4e1713SPravin B Shelar rtnl_lock(); 167be4ace6eSThomas Graf if (vport->dev->priv_flags & IFF_OVS_DATAPATH) 168b07c2651SAlexei Starovoitov ovs_netdev_detach_dev(vport); 1698e4e1713SPravin B Shelar rtnl_unlock(); 170ccb1352eSJesse Gross 171be4ace6eSThomas Graf call_rcu(&vport->rcu, free_port_rcu); 172ccb1352eSJesse Gross } 173ccb1352eSJesse Gross 174ccb1352eSJesse Gross const char *ovs_netdev_get_name(const struct vport *vport) 175ccb1352eSJesse Gross { 176be4ace6eSThomas Graf return vport->dev->name; 177ccb1352eSJesse Gross } 178ccb1352eSJesse Gross 17995c96174SEric Dumazet static unsigned int packet_length(const struct sk_buff *skb) 180ccb1352eSJesse Gross { 18195c96174SEric Dumazet unsigned int length = skb->len - ETH_HLEN; 182ccb1352eSJesse Gross 183ccb1352eSJesse Gross if (skb->protocol == htons(ETH_P_8021Q)) 184ccb1352eSJesse Gross length -= VLAN_HLEN; 185ccb1352eSJesse Gross 186ccb1352eSJesse Gross return length; 187ccb1352eSJesse Gross } 188ccb1352eSJesse Gross 189ccb1352eSJesse Gross static int netdev_send(struct vport *vport, struct sk_buff *skb) 190ccb1352eSJesse Gross { 191be4ace6eSThomas Graf int mtu = vport->dev->mtu; 192ccb1352eSJesse Gross int len; 193ccb1352eSJesse Gross 194ccb1352eSJesse Gross if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) { 195e87cc472SJoe Perches net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n", 196be4ace6eSThomas Graf vport->dev->name, 197e87cc472SJoe Perches packet_length(skb), mtu); 19891b7514cSPravin B Shelar goto drop; 199ccb1352eSJesse Gross } 200ccb1352eSJesse Gross 201be4ace6eSThomas Graf skb->dev = vport->dev; 202ccb1352eSJesse Gross len = skb->len; 203ccb1352eSJesse Gross dev_queue_xmit(skb); 204ccb1352eSJesse Gross 205ccb1352eSJesse Gross return len; 206ccb1352eSJesse Gross 20791b7514cSPravin B Shelar drop: 208ccb1352eSJesse Gross kfree_skb(skb); 209ccb1352eSJesse Gross return 0; 210ccb1352eSJesse Gross } 211ccb1352eSJesse Gross 212ccb1352eSJesse Gross /* Returns null if this device is not attached to a datapath. */ 213ccb1352eSJesse Gross struct vport *ovs_netdev_get_vport(struct net_device *dev) 214ccb1352eSJesse Gross { 215ccb1352eSJesse Gross if (likely(dev->priv_flags & IFF_OVS_DATAPATH)) 216ccb1352eSJesse Gross return (struct vport *) 217ccb1352eSJesse Gross rcu_dereference_rtnl(dev->rx_handler_data); 218ccb1352eSJesse Gross else 219ccb1352eSJesse Gross return NULL; 220ccb1352eSJesse Gross } 221ccb1352eSJesse Gross 22262b9c8d0SThomas Graf static struct vport_ops ovs_netdev_vport_ops = { 223ccb1352eSJesse Gross .type = OVS_VPORT_TYPE_NETDEV, 224ccb1352eSJesse Gross .create = netdev_create, 225ccb1352eSJesse Gross .destroy = netdev_destroy, 226ccb1352eSJesse Gross .get_name = ovs_netdev_get_name, 227ccb1352eSJesse Gross .send = netdev_send, 228ccb1352eSJesse Gross }; 22962b9c8d0SThomas Graf 23062b9c8d0SThomas Graf int __init ovs_netdev_init(void) 23162b9c8d0SThomas Graf { 23262b9c8d0SThomas Graf return ovs_vport_ops_register(&ovs_netdev_vport_ops); 23362b9c8d0SThomas Graf } 23462b9c8d0SThomas Graf 23562b9c8d0SThomas Graf void ovs_netdev_exit(void) 23662b9c8d0SThomas Graf { 23762b9c8d0SThomas Graf ovs_vport_ops_unregister(&ovs_netdev_vport_ops); 23862b9c8d0SThomas Graf } 239