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 36*62b9c8d0SThomas Graf static struct vport_ops ovs_netdev_vport_ops; 37*62b9c8d0SThomas 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 802537b4ddSJiri Pirko static struct net_device *get_dpdev(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); 862537b4ddSJiri Pirko return netdev_vport_priv(local)->dev; 872537b4ddSJiri Pirko } 882537b4ddSJiri Pirko 89ccb1352eSJesse Gross static struct vport *netdev_create(const struct vport_parms *parms) 90ccb1352eSJesse Gross { 91ccb1352eSJesse Gross struct vport *vport; 92ccb1352eSJesse Gross struct netdev_vport *netdev_vport; 93ccb1352eSJesse Gross int err; 94ccb1352eSJesse Gross 95ccb1352eSJesse Gross vport = ovs_vport_alloc(sizeof(struct netdev_vport), 96ccb1352eSJesse Gross &ovs_netdev_vport_ops, parms); 97ccb1352eSJesse Gross if (IS_ERR(vport)) { 98ccb1352eSJesse Gross err = PTR_ERR(vport); 99ccb1352eSJesse Gross goto error; 100ccb1352eSJesse Gross } 101ccb1352eSJesse Gross 102ccb1352eSJesse Gross netdev_vport = netdev_vport_priv(vport); 103ccb1352eSJesse Gross 10446df7b81SPravin B Shelar netdev_vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), parms->name); 105ccb1352eSJesse Gross if (!netdev_vport->dev) { 106ccb1352eSJesse Gross err = -ENODEV; 107ccb1352eSJesse Gross goto error_free_vport; 108ccb1352eSJesse Gross } 109ccb1352eSJesse Gross 110ccb1352eSJesse Gross if (netdev_vport->dev->flags & IFF_LOOPBACK || 111ccb1352eSJesse Gross netdev_vport->dev->type != ARPHRD_ETHER || 112ccb1352eSJesse Gross ovs_is_internal_dev(netdev_vport->dev)) { 113ccb1352eSJesse Gross err = -EINVAL; 114ccb1352eSJesse Gross goto error_put; 115ccb1352eSJesse Gross } 116ccb1352eSJesse Gross 1178e4e1713SPravin B Shelar rtnl_lock(); 1182537b4ddSJiri Pirko err = netdev_master_upper_dev_link(netdev_vport->dev, 1192537b4ddSJiri Pirko get_dpdev(vport->dp)); 1202537b4ddSJiri Pirko if (err) 1212537b4ddSJiri Pirko goto error_unlock; 1222537b4ddSJiri Pirko 123ccb1352eSJesse Gross err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook, 124ccb1352eSJesse Gross vport); 125ccb1352eSJesse Gross if (err) 1262537b4ddSJiri Pirko goto error_master_upper_dev_unlink; 127ccb1352eSJesse Gross 128ccb1352eSJesse Gross dev_set_promiscuity(netdev_vport->dev, 1); 129ccb1352eSJesse Gross netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH; 1308e4e1713SPravin B Shelar rtnl_unlock(); 131ccb1352eSJesse Gross 132ccb1352eSJesse Gross return vport; 133ccb1352eSJesse Gross 1342537b4ddSJiri Pirko error_master_upper_dev_unlink: 1352537b4ddSJiri Pirko netdev_upper_dev_unlink(netdev_vport->dev, get_dpdev(vport->dp)); 1368e4e1713SPravin B Shelar error_unlock: 1378e4e1713SPravin B Shelar rtnl_unlock(); 138ccb1352eSJesse Gross error_put: 139ccb1352eSJesse Gross dev_put(netdev_vport->dev); 140ccb1352eSJesse Gross error_free_vport: 141ccb1352eSJesse Gross ovs_vport_free(vport); 142ccb1352eSJesse Gross error: 143ccb1352eSJesse Gross return ERR_PTR(err); 144ccb1352eSJesse Gross } 145ccb1352eSJesse Gross 14692eb1d47SJesse Gross static void free_port_rcu(struct rcu_head *rcu) 14792eb1d47SJesse Gross { 14892eb1d47SJesse Gross struct netdev_vport *netdev_vport = container_of(rcu, 14992eb1d47SJesse Gross struct netdev_vport, rcu); 15092eb1d47SJesse Gross 15192eb1d47SJesse Gross dev_put(netdev_vport->dev); 15292eb1d47SJesse Gross ovs_vport_free(vport_from_priv(netdev_vport)); 15392eb1d47SJesse Gross } 15492eb1d47SJesse Gross 155b07c2651SAlexei Starovoitov void ovs_netdev_detach_dev(struct vport *vport) 156b07c2651SAlexei Starovoitov { 157b07c2651SAlexei Starovoitov struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 158b07c2651SAlexei Starovoitov 159b07c2651SAlexei Starovoitov ASSERT_RTNL(); 160b07c2651SAlexei Starovoitov netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH; 161b07c2651SAlexei Starovoitov netdev_rx_handler_unregister(netdev_vport->dev); 162b07c2651SAlexei Starovoitov netdev_upper_dev_unlink(netdev_vport->dev, 163b07c2651SAlexei Starovoitov netdev_master_upper_dev_get(netdev_vport->dev)); 164b07c2651SAlexei Starovoitov dev_set_promiscuity(netdev_vport->dev, -1); 165b07c2651SAlexei Starovoitov } 166b07c2651SAlexei Starovoitov 167ccb1352eSJesse Gross static void netdev_destroy(struct vport *vport) 168ccb1352eSJesse Gross { 169ccb1352eSJesse Gross struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 170ccb1352eSJesse Gross 1718e4e1713SPravin B Shelar rtnl_lock(); 172b07c2651SAlexei Starovoitov if (netdev_vport->dev->priv_flags & IFF_OVS_DATAPATH) 173b07c2651SAlexei Starovoitov ovs_netdev_detach_dev(vport); 1748e4e1713SPravin B Shelar rtnl_unlock(); 175ccb1352eSJesse Gross 17692eb1d47SJesse Gross call_rcu(&netdev_vport->rcu, free_port_rcu); 177ccb1352eSJesse Gross } 178ccb1352eSJesse Gross 179ccb1352eSJesse Gross const char *ovs_netdev_get_name(const struct vport *vport) 180ccb1352eSJesse Gross { 181ccb1352eSJesse Gross const struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 182ccb1352eSJesse Gross return netdev_vport->dev->name; 183ccb1352eSJesse Gross } 184ccb1352eSJesse Gross 18595c96174SEric Dumazet static unsigned int packet_length(const struct sk_buff *skb) 186ccb1352eSJesse Gross { 18795c96174SEric Dumazet unsigned int length = skb->len - ETH_HLEN; 188ccb1352eSJesse Gross 189ccb1352eSJesse Gross if (skb->protocol == htons(ETH_P_8021Q)) 190ccb1352eSJesse Gross length -= VLAN_HLEN; 191ccb1352eSJesse Gross 192ccb1352eSJesse Gross return length; 193ccb1352eSJesse Gross } 194ccb1352eSJesse Gross 195ccb1352eSJesse Gross static int netdev_send(struct vport *vport, struct sk_buff *skb) 196ccb1352eSJesse Gross { 197ccb1352eSJesse Gross struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 198ccb1352eSJesse Gross int mtu = netdev_vport->dev->mtu; 199ccb1352eSJesse Gross int len; 200ccb1352eSJesse Gross 201ccb1352eSJesse Gross if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) { 202e87cc472SJoe Perches net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n", 203c1c92b6aSJesse Gross netdev_vport->dev->name, 204e87cc472SJoe Perches packet_length(skb), mtu); 20591b7514cSPravin B Shelar goto drop; 206ccb1352eSJesse Gross } 207ccb1352eSJesse Gross 208ccb1352eSJesse Gross skb->dev = netdev_vport->dev; 209ccb1352eSJesse Gross len = skb->len; 210ccb1352eSJesse Gross dev_queue_xmit(skb); 211ccb1352eSJesse Gross 212ccb1352eSJesse Gross return len; 213ccb1352eSJesse Gross 21491b7514cSPravin B Shelar drop: 215ccb1352eSJesse Gross kfree_skb(skb); 216ccb1352eSJesse Gross return 0; 217ccb1352eSJesse Gross } 218ccb1352eSJesse Gross 219ccb1352eSJesse Gross /* Returns null if this device is not attached to a datapath. */ 220ccb1352eSJesse Gross struct vport *ovs_netdev_get_vport(struct net_device *dev) 221ccb1352eSJesse Gross { 222ccb1352eSJesse Gross if (likely(dev->priv_flags & IFF_OVS_DATAPATH)) 223ccb1352eSJesse Gross return (struct vport *) 224ccb1352eSJesse Gross rcu_dereference_rtnl(dev->rx_handler_data); 225ccb1352eSJesse Gross else 226ccb1352eSJesse Gross return NULL; 227ccb1352eSJesse Gross } 228ccb1352eSJesse Gross 229*62b9c8d0SThomas Graf static struct vport_ops ovs_netdev_vport_ops = { 230ccb1352eSJesse Gross .type = OVS_VPORT_TYPE_NETDEV, 231ccb1352eSJesse Gross .create = netdev_create, 232ccb1352eSJesse Gross .destroy = netdev_destroy, 233ccb1352eSJesse Gross .get_name = ovs_netdev_get_name, 234ccb1352eSJesse Gross .send = netdev_send, 235ccb1352eSJesse Gross }; 236*62b9c8d0SThomas Graf 237*62b9c8d0SThomas Graf int __init ovs_netdev_init(void) 238*62b9c8d0SThomas Graf { 239*62b9c8d0SThomas Graf return ovs_vport_ops_register(&ovs_netdev_vport_ops); 240*62b9c8d0SThomas Graf } 241*62b9c8d0SThomas Graf 242*62b9c8d0SThomas Graf void ovs_netdev_exit(void) 243*62b9c8d0SThomas Graf { 244*62b9c8d0SThomas Graf ovs_vport_ops_unregister(&ovs_netdev_vport_ops); 245*62b9c8d0SThomas Graf } 246