2 * Copyright (c) 2007-2012 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/if_arp.h>
22 #include <linux/if_bridge.h>
23 #include <linux/if_vlan.h>
24 #include <linux/kernel.h>
25 #include <linux/llc.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/skbuff.h>
28 #include <linux/openvswitch.h>
31 #include <net/ip_tunnels.h>
32 #include <net/rtnetlink.h>
33 #include <net/vxlan.h>
37 #include "vport-internal_dev.h"
38 #include "vport-netdev.h"
40 static struct vport_ops ovs_netdev_vport_ops;
42 /* Must be called with rcu_read_lock. */
43 static void netdev_port_receive(struct vport *vport, struct sk_buff *skb)
48 if (unlikely(skb_warn_if_lro(skb)))
51 /* Make our own copy of the packet. Otherwise we will mangle the
52 * packet for anyone who came before us (e.g. tcpdump via AF_PACKET).
54 skb = skb_share_check(skb, GFP_ATOMIC);
58 skb_push(skb, ETH_HLEN);
59 ovs_skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
61 ovs_vport_receive(vport, skb, skb_tunnel_info(skb, AF_INET));
68 /* Called with rcu_read_lock and bottom-halves disabled. */
69 static rx_handler_result_t netdev_frame_hook(struct sk_buff **pskb)
71 struct sk_buff *skb = *pskb;
74 if (unlikely(skb->pkt_type == PACKET_LOOPBACK))
75 return RX_HANDLER_PASS;
77 vport = ovs_netdev_get_vport(skb->dev);
79 netdev_port_receive(vport, skb);
81 return RX_HANDLER_CONSUMED;
84 static struct net_device *get_dpdev(const struct datapath *dp)
88 local = ovs_vport_ovsl(dp, OVSP_LOCAL);
93 static struct vport *netdev_link(struct vport *vport, const char *name)
97 vport->dev = dev_get_by_name(ovs_dp_get_net(vport->dp), name);
100 goto error_free_vport;
103 if (vport->dev->flags & IFF_LOOPBACK ||
104 vport->dev->type != ARPHRD_ETHER ||
105 ovs_is_internal_dev(vport->dev)) {
111 err = netdev_master_upper_dev_link(vport->dev,
112 get_dpdev(vport->dp));
116 err = netdev_rx_handler_register(vport->dev, netdev_frame_hook,
119 goto error_master_upper_dev_unlink;
121 dev_disable_lro(vport->dev);
122 dev_set_promiscuity(vport->dev, 1);
123 vport->dev->priv_flags |= IFF_OVS_DATAPATH;
128 error_master_upper_dev_unlink:
129 netdev_upper_dev_unlink(vport->dev, get_dpdev(vport->dp));
135 ovs_vport_free(vport);
139 static struct vport *netdev_create(const struct vport_parms *parms)
143 vport = ovs_vport_alloc(0, &ovs_netdev_vport_ops, parms);
147 return netdev_link(vport, parms->name);
150 static void free_port_rcu(struct rcu_head *rcu)
152 struct vport *vport = container_of(rcu, struct vport, rcu);
156 ovs_vport_free(vport);
159 void ovs_netdev_detach_dev(struct vport *vport)
162 vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
163 netdev_rx_handler_unregister(vport->dev);
164 netdev_upper_dev_unlink(vport->dev,
165 netdev_master_upper_dev_get(vport->dev));
166 dev_set_promiscuity(vport->dev, -1);
169 static void netdev_destroy(struct vport *vport)
172 if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
173 ovs_netdev_detach_dev(vport);
176 call_rcu(&vport->rcu, free_port_rcu);
179 static unsigned int packet_length(const struct sk_buff *skb)
181 unsigned int length = skb->len - ETH_HLEN;
183 if (skb->protocol == htons(ETH_P_8021Q))
189 static int netdev_send(struct vport *vport, struct sk_buff *skb)
191 int mtu = vport->dev->mtu;
194 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
195 net_warn_ratelimited("%s: dropped over-mtu packet: %d > %d\n",
197 packet_length(skb), mtu);
201 skb->dev = vport->dev;
212 /* Returns null if this device is not attached to a datapath. */
213 struct vport *ovs_netdev_get_vport(struct net_device *dev)
215 if (likely(dev->priv_flags & IFF_OVS_DATAPATH))
216 return (struct vport *)
217 rcu_dereference_rtnl(dev->rx_handler_data);
222 static struct vport_ops ovs_netdev_vport_ops = {
223 .type = OVS_VPORT_TYPE_NETDEV,
224 .create = netdev_create,
225 .destroy = netdev_destroy,
229 /* Compat code for old userspace. */
230 #if IS_ENABLED(CONFIG_VXLAN)
231 static struct vport_ops ovs_vxlan_netdev_vport_ops;
233 static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
235 struct vxlan_dev *vxlan = netdev_priv(vport->dev);
236 __be16 dst_port = vxlan->cfg.dst_port;
238 if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
241 if (vxlan->flags & VXLAN_F_GBP) {
244 exts = nla_nest_start(skb, OVS_TUNNEL_ATTR_EXTENSION);
248 if (vxlan->flags & VXLAN_F_GBP &&
249 nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
252 nla_nest_end(skb, exts);
258 static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX + 1] = {
259 [OVS_VXLAN_EXT_GBP] = { .type = NLA_FLAG, },
262 static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr,
263 struct vxlan_config *conf)
265 struct nlattr *exts[OVS_VXLAN_EXT_MAX + 1];
268 if (nla_len(attr) < sizeof(struct nlattr))
271 err = nla_parse_nested(exts, OVS_VXLAN_EXT_MAX, attr, exts_policy);
275 if (exts[OVS_VXLAN_EXT_GBP])
276 conf->flags |= VXLAN_F_GBP;
281 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
283 struct net *net = ovs_dp_get_net(parms->dp);
284 struct nlattr *options = parms->options;
285 struct net_device *dev;
289 struct vxlan_config conf = {
291 .flags = VXLAN_F_FLOW_BASED | VXLAN_F_COLLECT_METADATA,
299 a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
300 if (a && nla_len(a) == sizeof(u16)) {
301 conf.dst_port = htons(nla_get_u16(a));
303 /* Require destination port from userspace. */
308 vport = ovs_vport_alloc(0, &ovs_vxlan_netdev_vport_ops, parms);
312 a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
314 err = vxlan_configure_exts(vport, a, &conf);
316 ovs_vport_free(vport);
322 dev = vxlan_dev_create(net, parms->name, NET_NAME_USER, &conf);
325 ovs_vport_free(vport);
326 return ERR_CAST(dev);
329 dev_change_flags(dev, dev->flags | IFF_UP);
336 static struct vport *vxlan_create(const struct vport_parms *parms)
340 vport = vxlan_tnl_create(parms);
344 return netdev_link(vport, parms->name);
347 static void vxlan_destroy(struct vport *vport)
350 if (vport->dev->priv_flags & IFF_OVS_DATAPATH)
351 ovs_netdev_detach_dev(vport);
353 /* Early release so we can unregister the device */
355 rtnl_delete_link(vport->dev);
359 call_rcu(&vport->rcu, free_port_rcu);
362 static int vxlan_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
363 struct ip_tunnel_info *egress_tun_info)
365 struct vxlan_dev *vxlan = netdev_priv(vport->dev);
366 struct net *net = ovs_dp_get_net(vport->dp);
367 __be16 dst_port = vxlan_dev_dst_port(vxlan);
372 inet_get_local_port_range(net, &port_min, &port_max);
373 src_port = udp_flow_src_port(net, skb, 0, 0, true);
375 return ovs_tunnel_get_egress_info(egress_tun_info, net,
376 OVS_CB(skb)->egress_tun_info,
377 IPPROTO_UDP, skb->mark,
381 static struct vport_ops ovs_vxlan_netdev_vport_ops = {
382 .type = OVS_VPORT_TYPE_VXLAN,
383 .create = vxlan_create,
384 .destroy = vxlan_destroy,
385 .get_options = vxlan_get_options,
387 .get_egress_tun_info = vxlan_get_egress_tun_info,
390 static int vxlan_compat_init(void)
392 return ovs_vport_ops_register(&ovs_vxlan_netdev_vport_ops);
395 static void vxlan_compat_exit(void)
397 ovs_vport_ops_unregister(&ovs_vxlan_netdev_vport_ops);
400 static int vxlan_compat_init(void)
405 static void vxlan_compat_exit(void)
410 int __init ovs_netdev_init(void)
414 err = ovs_vport_ops_register(&ovs_netdev_vport_ops);
417 err = vxlan_compat_init();
423 void ovs_netdev_exit(void)
425 ovs_vport_ops_unregister(&ovs_netdev_vport_ops);