^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 2014 Nicira, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (c) 2013 Cisco Systems, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/openvswitch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <net/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <net/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <net/vxlan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "datapath.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "vport.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "vport-netdev.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct vport_ops ovs_vxlan_netdev_vport_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int vxlan_get_options(const struct vport *vport, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct vxlan_dev *vxlan = netdev_priv(vport->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) __be16 dst_port = vxlan->cfg.dst_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, ntohs(dst_port)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (vxlan->cfg.flags & VXLAN_F_GBP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct nlattr *exts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) exts = nla_nest_start_noflag(skb, OVS_TUNNEL_ATTR_EXTENSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (!exts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (vxlan->cfg.flags & VXLAN_F_GBP &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) nla_put_flag(skb, OVS_VXLAN_EXT_GBP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -EMSGSIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) nla_nest_end(skb, exts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static const struct nla_policy exts_policy[OVS_VXLAN_EXT_MAX + 1] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) [OVS_VXLAN_EXT_GBP] = { .type = NLA_FLAG, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int vxlan_configure_exts(struct vport *vport, struct nlattr *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct vxlan_config *conf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct nlattr *exts[OVS_VXLAN_EXT_MAX + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (nla_len(attr) < sizeof(struct nlattr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) err = nla_parse_nested_deprecated(exts, OVS_VXLAN_EXT_MAX, attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) exts_policy, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (exts[OVS_VXLAN_EXT_GBP])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) conf->flags |= VXLAN_F_GBP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static struct vport *vxlan_tnl_create(const struct vport_parms *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct net *net = ovs_dp_get_net(parms->dp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct nlattr *options = parms->options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct net_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct vport *vport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct nlattr *a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct vxlan_config conf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .no_share = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .flags = VXLAN_F_COLLECT_METADATA | VXLAN_F_UDP_ZERO_CSUM6_RX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /* Don't restrict the packets that can be sent by MTU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .mtu = IP_MAX_MTU,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (a && nla_len(a) == sizeof(u16)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) conf.dst_port = htons(nla_get_u16(a));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /* Require destination port from userspace. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) vport = ovs_vport_alloc(0, &ovs_vxlan_netdev_vport_ops, parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (IS_ERR(vport))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return vport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) a = nla_find_nested(options, OVS_TUNNEL_ATTR_EXTENSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (a) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) err = vxlan_configure_exts(vport, a, &conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ovs_vport_free(vport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) dev = vxlan_dev_create(net, parms->name, NET_NAME_USER, &conf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (IS_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) ovs_vport_free(vport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return ERR_CAST(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) err = dev_change_flags(dev, dev->flags | IFF_UP, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) rtnl_delete_link(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ovs_vport_free(vport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return vport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static struct vport *vxlan_create(const struct vport_parms *parms)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct vport *vport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) vport = vxlan_tnl_create(parms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (IS_ERR(vport))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return vport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return ovs_netdev_link(vport, parms->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static struct vport_ops ovs_vxlan_netdev_vport_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) .type = OVS_VPORT_TYPE_VXLAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) .create = vxlan_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) .destroy = ovs_netdev_tunnel_destroy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .get_options = vxlan_get_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) .send = dev_queue_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int __init ovs_vxlan_tnl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return ovs_vport_ops_register(&ovs_vxlan_netdev_vport_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static void __exit ovs_vxlan_tnl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ovs_vport_ops_unregister(&ovs_vxlan_netdev_vport_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) module_init(ovs_vxlan_tnl_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) module_exit(ovs_vxlan_tnl_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) MODULE_DESCRIPTION("OVS: VXLAN switching port");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) MODULE_ALIAS("vport-type-4");