Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2014 Nicira, Inc.
0004  * Copyright (c) 2013 Cisco Systems, Inc.
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/skbuff.h>
0009 #include <linux/openvswitch.h>
0010 #include <linux/module.h>
0011 #include <net/udp.h>
0012 #include <net/ip_tunnels.h>
0013 #include <net/rtnetlink.h>
0014 #include <net/vxlan.h>
0015 
0016 #include "datapath.h"
0017 #include "vport.h"
0018 #include "vport-netdev.h"
0019 
0020 static struct vport_ops ovs_vxlan_netdev_vport_ops;
0021 
0022 static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
0023 {
0024     struct vxlan_dev *vxlan = netdev_priv(vport->dev);
0025     __be16 dst_port = vxlan->cfg.dst_port;
0026 
0027     if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
0028         return -EMSGSIZE;
0029 
0030     if (vxlan->cfg.flags & VXLAN_F_GBP) {
0031         struct nlattr *exts;
0032 
0033         exts = nla_nest_start_noflag(skb, OVS_TUNNEL_ATTR_EXTENSION);
0034         if (!exts)
0035             return -EMSGSIZE;
0036 
0037         if (vxlan->cfg.flags & VXLAN_F_GBP &&
0038             nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
0039             return -EMSGSIZE;
0040 
0041         nla_nest_end(skb, exts);
0042     }
0043 
0044     return 0;
0045 }
0046 
0047 static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX + 1] = {
0048     [OVS_VXLAN_EXT_GBP] = { .type = NLA_FLAG, },
0049 };
0050 
0051 static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr,
0052                 struct vxlan_config *conf)
0053 {
0054     struct nlattr *exts[OVS_VXLAN_EXT_MAX + 1];
0055     int err;
0056 
0057     if (nla_len(attr) < sizeof(struct nlattr))
0058         return -EINVAL;
0059 
0060     err = nla_parse_nested_deprecated(exts, OVS_VXLAN_EXT_MAX, attr,
0061                       exts_policy, NULL);
0062     if (err < 0)
0063         return err;
0064 
0065     if (exts[OVS_VXLAN_EXT_GBP])
0066         conf->flags |= VXLAN_F_GBP;
0067 
0068     return 0;
0069 }
0070 
0071 static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
0072 {
0073     struct net *net = ovs_dp_get_net(parms->dp);
0074     struct nlattr *options = parms->options;
0075     struct net_device *dev;
0076     struct vport *vport;
0077     struct nlattr *a;
0078     int err;
0079     struct vxlan_config conf = {
0080         .no_share = true,
0081         .flags = VXLAN_F_COLLECT_METADATA | VXLAN_F_UDP_ZERO_CSUM6_RX,
0082         /* Don't restrict the packets that can be sent by MTU */
0083         .mtu = IP_MAX_MTU,
0084     };
0085 
0086     if (!options) {
0087         err = -EINVAL;
0088         goto error;
0089     }
0090 
0091     a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
0092     if (a && nla_len(a) == sizeof(u16)) {
0093         conf.dst_port = htons(nla_get_u16(a));
0094     } else {
0095         /* Require destination port from userspace. */
0096         err = -EINVAL;
0097         goto error;
0098     }
0099 
0100     vport = ovs_vport_alloc(0, &ovs_vxlan_netdev_vport_ops, parms);
0101     if (IS_ERR(vport))
0102         return vport;
0103 
0104     a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
0105     if (a) {
0106         err = vxlan_configure_exts(vport, a, &conf);
0107         if (err) {
0108             ovs_vport_free(vport);
0109             goto error;
0110         }
0111     }
0112 
0113     rtnl_lock();
0114     dev = vxlan_dev_create(net, parms->name, NET_NAME_USER, &conf);
0115     if (IS_ERR(dev)) {
0116         rtnl_unlock();
0117         ovs_vport_free(vport);
0118         return ERR_CAST(dev);
0119     }
0120 
0121     err = dev_change_flags(dev, dev->flags | IFF_UP, NULL);
0122     if (err < 0) {
0123         rtnl_delete_link(dev);
0124         rtnl_unlock();
0125         ovs_vport_free(vport);
0126         goto error;
0127     }
0128 
0129     rtnl_unlock();
0130     return vport;
0131 error:
0132     return ERR_PTR(err);
0133 }
0134 
0135 static struct vport *vxlan_create(const struct vport_parms *parms)
0136 {
0137     struct vport *vport;
0138 
0139     vport = vxlan_tnl_create(parms);
0140     if (IS_ERR(vport))
0141         return vport;
0142 
0143     return ovs_netdev_link(vport, parms->name);
0144 }
0145 
0146 static struct vport_ops ovs_vxlan_netdev_vport_ops = {
0147     .type           = OVS_VPORT_TYPE_VXLAN,
0148     .create         = vxlan_create,
0149     .destroy        = ovs_netdev_tunnel_destroy,
0150     .get_options        = vxlan_get_options,
0151     .send           = dev_queue_xmit,
0152 };
0153 
0154 static int __init ovs_vxlan_tnl_init(void)
0155 {
0156     return ovs_vport_ops_register(&ovs_vxlan_netdev_vport_ops);
0157 }
0158 
0159 static void __exit ovs_vxlan_tnl_exit(void)
0160 {
0161     ovs_vport_ops_unregister(&ovs_vxlan_netdev_vport_ops);
0162 }
0163 
0164 module_init(ovs_vxlan_tnl_init);
0165 module_exit(ovs_vxlan_tnl_exit);
0166 
0167 MODULE_DESCRIPTION("OVS: VXLAN switching port");
0168 MODULE_LICENSE("GPL");
0169 MODULE_ALIAS("vport-type-4");