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>
29dcc38c03SThomas Graf #include <linux/export.h>
30ccb1352eSJesse Gross 
31614732eaSThomas Graf #include <net/ip_tunnels.h>
32614732eaSThomas Graf #include <net/rtnetlink.h>
33ccb1352eSJesse Gross 
34ccb1352eSJesse Gross #include "datapath.h"
35614732eaSThomas Graf #include "vport.h"
36ccb1352eSJesse Gross #include "vport-internal_dev.h"
37ccb1352eSJesse Gross #include "vport-netdev.h"
38ccb1352eSJesse Gross 
3962b9c8d0SThomas Graf static struct vport_ops ovs_netdev_vport_ops;
4062b9c8d0SThomas Graf 
41ccb1352eSJesse Gross /* Must be called with rcu_read_lock. */
42ccb1352eSJesse Gross static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
43ccb1352eSJesse Gross {
44d9d59089SJesse Gross 	if (unlikely(!vport))
45d9d59089SJesse Gross 		goto error;
46d9d59089SJesse Gross 
47d9d59089SJesse Gross 	if (unlikely(skb_warn_if_lro(skb)))
48d9d59089SJesse Gross 		goto error;
49ccb1352eSJesse Gross 
50ccb1352eSJesse Gross 	/* Make our own copy of the packet.  Otherwise we will mangle the
51ccb1352eSJesse Gross 	 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
52d176ca2aSCong Wang 	 */
53ccb1352eSJesse Gross 	skb = skb_share_check(skb, GFP_ATOMIC);
54ccb1352eSJesse Gross 	if (unlikely(!skb))
55ccb1352eSJesse Gross 		return;
56ccb1352eSJesse Gross 
57ccb1352eSJesse Gross 	skb_push(skb, ETH_HLEN);
58b34df5e8SPravin B Shelar 	ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
59b34df5e8SPravin B Shelar 
60597798e4SThomas Graf 	ovs_vport_receive(vport, skb, skb_tunnel_info(skb, AF_INET));
61d9d59089SJesse Gross 	return;
62d9d59089SJesse Gross 
63d9d59089SJesse Gross error:
64d9d59089SJesse Gross 	kfree_skb(skb);
65ccb1352eSJesse Gross }
66ccb1352eSJesse Gross 
67ccb1352eSJesse Gross /* Called with rcu_read_lock and bottom-halves disabled. */
68ccb1352eSJesse Gross static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
69ccb1352eSJesse Gross {
70ccb1352eSJesse Gross 	struct sk_buff *skb = *pskb;
71ccb1352eSJesse Gross 	struct vport *vport;
72ccb1352eSJesse Gross 
73ccb1352eSJesse Gross 	if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
74ccb1352eSJesse Gross 		return RX_HANDLER_PASS;
75ccb1352eSJesse Gross 
76ccb1352eSJesse Gross 	vport = ovs_netdev_get_vport(skb->dev);
77ccb1352eSJesse Gross 
78ccb1352eSJesse Gross 	netdev_port_receive(vport, skb);
79ccb1352eSJesse Gross 
80ccb1352eSJesse Gross 	return RX_HANDLER_CONSUMED;
81ccb1352eSJesse Gross }
82ccb1352eSJesse Gross 
8312eb18f7SThomas Graf static struct net_device *get_dpdev(const struct datapath *dp)
842537b4ddSJiri Pirko {
852537b4ddSJiri Pirko 	struct vport *local;
862537b4ddSJiri Pirko 
872537b4ddSJiri Pirko 	local = ovs_vport_ovsl(dp, OVSP_LOCAL);
882537b4ddSJiri Pirko 	BUG_ON(!local);
89be4ace6eSThomas Graf 	return local->dev;
902537b4ddSJiri Pirko }
912537b4ddSJiri Pirko 
92dcc38c03SThomas Graf struct vport *ovs_netdev_link(struct vport *vport, const char *name)
93ccb1352eSJesse Gross {
94ccb1352eSJesse Gross 	int err;
95ccb1352eSJesse Gross 
96be4ace6eSThomas Graf 	vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
97be4ace6eSThomas Graf 	if (!vport->dev) {
98ccb1352eSJesse Gross 		err = -ENODEV;
99ccb1352eSJesse Gross 		goto error_free_vport;
100ccb1352eSJesse Gross 	}
101ccb1352eSJesse Gross 
102be4ace6eSThomas Graf 	if (vport->dev->flags & IFF_LOOPBACK ||
103be4ace6eSThomas Graf 	    vport->dev->type != ARPHRD_ETHER ||
104be4ace6eSThomas Graf 	    ovs_is_internal_dev(vport->dev)) {
105ccb1352eSJesse Gross 		err = -EINVAL;
106ccb1352eSJesse Gross 		goto error_put;
107ccb1352eSJesse Gross 	}
108ccb1352eSJesse Gross 
1098e4e1713SPravin B Shelar 	rtnl_lock();
110be4ace6eSThomas Graf 	err = netdev_master_upper_dev_link(vport->dev,
1112537b4ddSJiri Pirko 					   get_dpdev(vport->dp));
1122537b4ddSJiri Pirko 	if (err)
1132537b4ddSJiri Pirko 		goto error_unlock;
1142537b4ddSJiri Pirko 
115be4ace6eSThomas Graf 	err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
116ccb1352eSJesse Gross 					 vport);
117ccb1352eSJesse Gross 	if (err)
1182537b4ddSJiri Pirko 		goto error_master_upper_dev_unlink;
119ccb1352eSJesse Gross 
120be4ace6eSThomas Graf 	dev_disable_lro(vport->dev);
121be4ace6eSThomas Graf 	dev_set_promiscuity(vport->dev, 1);
122be4ace6eSThomas Graf 	vport->dev->priv_flags |= IFF_OVS_DATAPATH;
1238e4e1713SPravin B Shelar 	rtnl_unlock();
124ccb1352eSJesse Gross 
125ccb1352eSJesse Gross 	return vport;
126ccb1352eSJesse Gross 
1272537b4ddSJiri Pirko error_master_upper_dev_unlink:
128be4ace6eSThomas Graf 	netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
1298e4e1713SPravin B Shelar error_unlock:
1308e4e1713SPravin B Shelar 	rtnl_unlock();
131ccb1352eSJesse Gross error_put:
132be4ace6eSThomas Graf 	dev_put(vport->dev);
133ccb1352eSJesse Gross error_free_vport:
134ccb1352eSJesse Gross 	ovs_vport_free(vport);
135ccb1352eSJesse Gross 	return ERR_PTR(err);
136ccb1352eSJesse Gross }
137dcc38c03SThomas Graf EXPORT_SYMBOL_GPL(ovs_netdev_link);
138ccb1352eSJesse Gross 
139be4ace6eSThomas Graf static struct vport *netdev_create(const struct vport_parms *parms)
140be4ace6eSThomas Graf {
141be4ace6eSThomas Graf 	struct vport *vport;
142be4ace6eSThomas Graf 
143be4ace6eSThomas Graf 	vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
144be4ace6eSThomas Graf 	if (IS_ERR(vport))
145be4ace6eSThomas Graf 		return vport;
146be4ace6eSThomas Graf 
147dcc38c03SThomas Graf 	return ovs_netdev_link(vport, parms->name);
148be4ace6eSThomas Graf }
149be4ace6eSThomas Graf 
150dcc38c03SThomas Graf void ovs_vport_free_rcu(struct rcu_head *rcu)
15192eb1d47SJesse Gross {
152be4ace6eSThomas Graf 	struct vport *vport = container_of(rcu, struct vport, rcu);
15392eb1d47SJesse Gross 
154614732eaSThomas Graf 	if (vport->dev)
155be4ace6eSThomas Graf 		dev_put(vport->dev);
156be4ace6eSThomas Graf 	ovs_vport_free(vport);
15792eb1d47SJesse Gross }
158dcc38c03SThomas Graf EXPORT_SYMBOL_GPL(ovs_vport_free_rcu);
15992eb1d47SJesse Gross 
160b07c2651SAlexei Starovoitov void ovs_netdev_detach_dev(struct vport *vport)
161b07c2651SAlexei Starovoitov {
162b07c2651SAlexei Starovoitov 	ASSERT_RTNL();
163be4ace6eSThomas Graf 	vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
164be4ace6eSThomas Graf 	netdev_rx_handler_unregister(vport->dev);
165be4ace6eSThomas Graf 	netdev_upper_dev_unlink(vport->dev,
166be4ace6eSThomas Graf 				netdev_master_upper_dev_get(vport->dev));
167be4ace6eSThomas Graf 	dev_set_promiscuity(vport->dev, -1);
168b07c2651SAlexei Starovoitov }
169dcc38c03SThomas Graf EXPORT_SYMBOL_GPL(ovs_netdev_detach_dev);
170b07c2651SAlexei Starovoitov 
171ccb1352eSJesse Gross static void netdev_destroy(struct vport *vport)
172ccb1352eSJesse Gross {
1738e4e1713SPravin B Shelar 	rtnl_lock();
174be4ace6eSThomas Graf 	if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
175b07c2651SAlexei Starovoitov 		ovs_netdev_detach_dev(vport);
1768e4e1713SPravin B Shelar 	rtnl_unlock();
177ccb1352eSJesse Gross 
178dcc38c03SThomas Graf 	call_rcu(&vport->rcu, ovs_vport_free_rcu);
179ccb1352eSJesse Gross }
180ccb1352eSJesse Gross 
18195c96174SEric Dumazet static unsigned int packet_length(const struct sk_buff *skb)
182ccb1352eSJesse Gross {
18395c96174SEric Dumazet 	unsigned int length = skb->len - ETH_HLEN;
184ccb1352eSJesse Gross 
185ccb1352eSJesse Gross 	if (skb->protocol == htons(ETH_P_8021Q))
186ccb1352eSJesse Gross 		length -= VLAN_HLEN;
187ccb1352eSJesse Gross 
188ccb1352eSJesse Gross 	return length;
189ccb1352eSJesse Gross }
190ccb1352eSJesse Gross 
191dcc38c03SThomas Graf int ovs_netdev_send(struct vport *vport, struct sk_buff *skb)
192ccb1352eSJesse Gross {
193be4ace6eSThomas Graf 	int mtu = vport->dev->mtu;
194ccb1352eSJesse Gross 	int len;
195ccb1352eSJesse Gross 
196ccb1352eSJesse Gross 	if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
197e87cc472SJoe Perches 		net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n",
198be4ace6eSThomas Graf 				     vport->dev->name,
199e87cc472SJoe Perches 				     packet_length(skb), mtu);
20091b7514cSPravin B Shelar 		goto drop;
201ccb1352eSJesse Gross 	}
202ccb1352eSJesse Gross 
203be4ace6eSThomas Graf 	skb->dev = vport->dev;
204ccb1352eSJesse Gross 	len = skb->len;
205ccb1352eSJesse Gross 	dev_queue_xmit(skb);
206ccb1352eSJesse Gross 
207ccb1352eSJesse Gross 	return len;
208ccb1352eSJesse Gross 
20991b7514cSPravin B Shelar drop:
210ccb1352eSJesse Gross 	kfree_skb(skb);
211ccb1352eSJesse Gross 	return 0;
212ccb1352eSJesse Gross }
213dcc38c03SThomas Graf EXPORT_SYMBOL_GPL(ovs_netdev_send);
214ccb1352eSJesse Gross 
215ccb1352eSJesse Gross /* Returns null if this device is not attached to a datapath. */
216ccb1352eSJesse Gross struct vport *ovs_netdev_get_vport(struct net_device *dev)
217ccb1352eSJesse Gross {
218ccb1352eSJesse Gross 	if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
219ccb1352eSJesse Gross 		return (struct vport *)
220ccb1352eSJesse Gross 			rcu_dereference_rtnl(dev->rx_handler_data);
221ccb1352eSJesse Gross 	else
222ccb1352eSJesse Gross 		return NULL;
223ccb1352eSJesse Gross }
224ccb1352eSJesse Gross 
22562b9c8d0SThomas Graf static struct vport_ops ovs_netdev_vport_ops = {
226ccb1352eSJesse Gross 	.type		= OVS_VPORT_TYPE_NETDEV,
227ccb1352eSJesse Gross 	.create		= netdev_create,
228ccb1352eSJesse Gross 	.destroy	= netdev_destroy,
229dcc38c03SThomas Graf 	.send		= ovs_netdev_send,
230ccb1352eSJesse Gross };
23162b9c8d0SThomas Graf 
23262b9c8d0SThomas Graf int __init ovs_netdev_init(void)
23362b9c8d0SThomas Graf {
234dcc38c03SThomas Graf 	return ovs_vport_ops_register(&ovs_netdev_vport_ops);
23562b9c8d0SThomas Graf }
23662b9c8d0SThomas Graf 
23762b9c8d0SThomas Graf void ovs_netdev_exit(void)
23862b9c8d0SThomas Graf {
23962b9c8d0SThomas Graf 	ovs_vport_ops_unregister(&ovs_netdev_vport_ops);
24062b9c8d0SThomas Graf }
241