1ccb1352eSJesse Gross /* 2*caf2ee14SRaju 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> 28ccb1352eSJesse Gross 29ccb1352eSJesse Gross #include <net/llc.h> 30ccb1352eSJesse Gross 31ccb1352eSJesse Gross #include "datapath.h" 32ccb1352eSJesse Gross #include "vport-internal_dev.h" 33ccb1352eSJesse Gross #include "vport-netdev.h" 34ccb1352eSJesse Gross 35ccb1352eSJesse Gross /* Must be called with rcu_read_lock. */ 36ccb1352eSJesse Gross static void netdev_port_receive(struct vport *vport, struct sk_buff *skb) 37ccb1352eSJesse Gross { 38ccb1352eSJesse Gross if (unlikely(!vport)) { 39ccb1352eSJesse Gross kfree_skb(skb); 40ccb1352eSJesse Gross return; 41ccb1352eSJesse Gross } 42ccb1352eSJesse Gross 43ccb1352eSJesse Gross /* Make our own copy of the packet. Otherwise we will mangle the 44ccb1352eSJesse Gross * packet for anyone who came before us (e.g. tcpdump via AF_PACKET). 45ccb1352eSJesse Gross * (No one comes after us, since we tell handle_bridge() that we took 46ccb1352eSJesse Gross * the packet.) */ 47ccb1352eSJesse Gross skb = skb_share_check(skb, GFP_ATOMIC); 48ccb1352eSJesse Gross if (unlikely(!skb)) 49ccb1352eSJesse Gross return; 50ccb1352eSJesse Gross 51ccb1352eSJesse Gross skb_push(skb, ETH_HLEN); 52ccb1352eSJesse Gross ovs_vport_receive(vport, skb); 53ccb1352eSJesse Gross } 54ccb1352eSJesse Gross 55ccb1352eSJesse Gross /* Called with rcu_read_lock and bottom-halves disabled. */ 56ccb1352eSJesse Gross static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb) 57ccb1352eSJesse Gross { 58ccb1352eSJesse Gross struct sk_buff *skb = *pskb; 59ccb1352eSJesse Gross struct vport *vport; 60ccb1352eSJesse Gross 61ccb1352eSJesse Gross if (unlikely(skb->pkt_type == PACKET_LOOPBACK)) 62ccb1352eSJesse Gross return RX_HANDLER_PASS; 63ccb1352eSJesse Gross 64ccb1352eSJesse Gross vport = ovs_netdev_get_vport(skb->dev); 65ccb1352eSJesse Gross 66ccb1352eSJesse Gross netdev_port_receive(vport, skb); 67ccb1352eSJesse Gross 68ccb1352eSJesse Gross return RX_HANDLER_CONSUMED; 69ccb1352eSJesse Gross } 70ccb1352eSJesse Gross 71ccb1352eSJesse Gross static struct vport *netdev_create(const struct vport_parms *parms) 72ccb1352eSJesse Gross { 73ccb1352eSJesse Gross struct vport *vport; 74ccb1352eSJesse Gross struct netdev_vport *netdev_vport; 75ccb1352eSJesse Gross int err; 76ccb1352eSJesse Gross 77ccb1352eSJesse Gross vport = ovs_vport_alloc(sizeof(struct netdev_vport), 78ccb1352eSJesse Gross &ovs_netdev_vport_ops, parms); 79ccb1352eSJesse Gross if (IS_ERR(vport)) { 80ccb1352eSJesse Gross err = PTR_ERR(vport); 81ccb1352eSJesse Gross goto error; 82ccb1352eSJesse Gross } 83ccb1352eSJesse Gross 84ccb1352eSJesse Gross netdev_vport = netdev_vport_priv(vport); 85ccb1352eSJesse Gross 86ccb1352eSJesse Gross netdev_vport->dev = dev_get_by_name(&init_net, parms->name); 87ccb1352eSJesse Gross if (!netdev_vport->dev) { 88ccb1352eSJesse Gross err = -ENODEV; 89ccb1352eSJesse Gross goto error_free_vport; 90ccb1352eSJesse Gross } 91ccb1352eSJesse Gross 92ccb1352eSJesse Gross if (netdev_vport->dev->flags & IFF_LOOPBACK || 93ccb1352eSJesse Gross netdev_vport->dev->type != ARPHRD_ETHER || 94ccb1352eSJesse Gross ovs_is_internal_dev(netdev_vport->dev)) { 95ccb1352eSJesse Gross err = -EINVAL; 96ccb1352eSJesse Gross goto error_put; 97ccb1352eSJesse Gross } 98ccb1352eSJesse Gross 99ccb1352eSJesse Gross err = netdev_rx_handler_register(netdev_vport->dev, netdev_frame_hook, 100ccb1352eSJesse Gross vport); 101ccb1352eSJesse Gross if (err) 102ccb1352eSJesse Gross goto error_put; 103ccb1352eSJesse Gross 104ccb1352eSJesse Gross dev_set_promiscuity(netdev_vport->dev, 1); 105ccb1352eSJesse Gross netdev_vport->dev->priv_flags |= IFF_OVS_DATAPATH; 106ccb1352eSJesse Gross 107ccb1352eSJesse Gross return vport; 108ccb1352eSJesse Gross 109ccb1352eSJesse Gross error_put: 110ccb1352eSJesse Gross dev_put(netdev_vport->dev); 111ccb1352eSJesse Gross error_free_vport: 112ccb1352eSJesse Gross ovs_vport_free(vport); 113ccb1352eSJesse Gross error: 114ccb1352eSJesse Gross return ERR_PTR(err); 115ccb1352eSJesse Gross } 116ccb1352eSJesse Gross 117ccb1352eSJesse Gross static void netdev_destroy(struct vport *vport) 118ccb1352eSJesse Gross { 119ccb1352eSJesse Gross struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 120ccb1352eSJesse Gross 121ccb1352eSJesse Gross netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH; 122ccb1352eSJesse Gross netdev_rx_handler_unregister(netdev_vport->dev); 123ccb1352eSJesse Gross dev_set_promiscuity(netdev_vport->dev, -1); 124ccb1352eSJesse Gross 125ccb1352eSJesse Gross synchronize_rcu(); 126ccb1352eSJesse Gross 127ccb1352eSJesse Gross dev_put(netdev_vport->dev); 128ccb1352eSJesse Gross ovs_vport_free(vport); 129ccb1352eSJesse Gross } 130ccb1352eSJesse Gross 131ccb1352eSJesse Gross const char *ovs_netdev_get_name(const struct vport *vport) 132ccb1352eSJesse Gross { 133ccb1352eSJesse Gross const struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 134ccb1352eSJesse Gross return netdev_vport->dev->name; 135ccb1352eSJesse Gross } 136ccb1352eSJesse Gross 137ccb1352eSJesse Gross int ovs_netdev_get_ifindex(const struct vport *vport) 138ccb1352eSJesse Gross { 139ccb1352eSJesse Gross const struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 140ccb1352eSJesse Gross return netdev_vport->dev->ifindex; 141ccb1352eSJesse Gross } 142ccb1352eSJesse Gross 143ccb1352eSJesse Gross static unsigned packet_length(const struct sk_buff *skb) 144ccb1352eSJesse Gross { 145ccb1352eSJesse Gross unsigned length = skb->len - ETH_HLEN; 146ccb1352eSJesse Gross 147ccb1352eSJesse Gross if (skb->protocol == htons(ETH_P_8021Q)) 148ccb1352eSJesse Gross length -= VLAN_HLEN; 149ccb1352eSJesse Gross 150ccb1352eSJesse Gross return length; 151ccb1352eSJesse Gross } 152ccb1352eSJesse Gross 153ccb1352eSJesse Gross static int netdev_send(struct vport *vport, struct sk_buff *skb) 154ccb1352eSJesse Gross { 155ccb1352eSJesse Gross struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 156ccb1352eSJesse Gross int mtu = netdev_vport->dev->mtu; 157ccb1352eSJesse Gross int len; 158ccb1352eSJesse Gross 159ccb1352eSJesse Gross if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) { 160ccb1352eSJesse Gross if (net_ratelimit()) 161ccb1352eSJesse Gross pr_warn("%s: dropped over-mtu packet: %d > %d\n", 162ccb1352eSJesse Gross ovs_dp_name(vport->dp), packet_length(skb), mtu); 163ccb1352eSJesse Gross goto error; 164ccb1352eSJesse Gross } 165ccb1352eSJesse Gross 166ccb1352eSJesse Gross if (unlikely(skb_warn_if_lro(skb))) 167ccb1352eSJesse Gross goto error; 168ccb1352eSJesse Gross 169ccb1352eSJesse Gross skb->dev = netdev_vport->dev; 170ccb1352eSJesse Gross len = skb->len; 171ccb1352eSJesse Gross dev_queue_xmit(skb); 172ccb1352eSJesse Gross 173ccb1352eSJesse Gross return len; 174ccb1352eSJesse Gross 175ccb1352eSJesse Gross error: 176ccb1352eSJesse Gross kfree_skb(skb); 177ccb1352eSJesse Gross ovs_vport_record_error(vport, VPORT_E_TX_DROPPED); 178ccb1352eSJesse Gross return 0; 179ccb1352eSJesse Gross } 180ccb1352eSJesse Gross 181ccb1352eSJesse Gross /* Returns null if this device is not attached to a datapath. */ 182ccb1352eSJesse Gross struct vport *ovs_netdev_get_vport(struct net_device *dev) 183ccb1352eSJesse Gross { 184ccb1352eSJesse Gross if (likely(dev->priv_flags & IFF_OVS_DATAPATH)) 185ccb1352eSJesse Gross return (struct vport *) 186ccb1352eSJesse Gross rcu_dereference_rtnl(dev->rx_handler_data); 187ccb1352eSJesse Gross else 188ccb1352eSJesse Gross return NULL; 189ccb1352eSJesse Gross } 190ccb1352eSJesse Gross 191ccb1352eSJesse Gross const struct vport_ops ovs_netdev_vport_ops = { 192ccb1352eSJesse Gross .type = OVS_VPORT_TYPE_NETDEV, 193ccb1352eSJesse Gross .create = netdev_create, 194ccb1352eSJesse Gross .destroy = netdev_destroy, 195ccb1352eSJesse Gross .get_name = ovs_netdev_get_name, 196ccb1352eSJesse Gross .get_ifindex = ovs_netdev_get_ifindex, 197ccb1352eSJesse Gross .send = netdev_send, 198ccb1352eSJesse Gross }; 199