xref: /linux/net/openvswitch/vport-geneve.c (revision eb3fcf007fffe5830d815e713591f3e858f2a365)
1 /*
2  * Copyright (c) 2014 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/in.h>
13 #include <linux/ip.h>
14 #include <linux/net.h>
15 #include <linux/rculist.h>
16 #include <linux/udp.h>
17 #include <linux/if_vlan.h>
18 #include <linux/module.h>
19 
20 #include <net/geneve.h>
21 #include <net/icmp.h>
22 #include <net/ip.h>
23 #include <net/route.h>
24 #include <net/udp.h>
25 #include <net/xfrm.h>
26 
27 #include "datapath.h"
28 #include "vport.h"
29 #include "vport-netdev.h"
30 
31 static struct vport_ops ovs_geneve_vport_ops;
32 /**
33  * struct geneve_port - Keeps track of open UDP ports
34  * @dst_port: destination port.
35  */
36 struct geneve_port {
37 	u16 port_no;
38 };
39 
40 static inline struct geneve_port *geneve_vport(const struct vport *vport)
41 {
42 	return vport_priv(vport);
43 }
44 
45 static int geneve_get_options(const struct vport *vport,
46 			      struct sk_buff *skb)
47 {
48 	struct geneve_port *geneve_port = geneve_vport(vport);
49 
50 	if (nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT, geneve_port->port_no))
51 		return -EMSGSIZE;
52 	return 0;
53 }
54 
55 static int geneve_get_egress_tun_info(struct vport *vport, struct sk_buff *skb,
56 				      struct dp_upcall_info *upcall)
57 {
58 	struct geneve_port *geneve_port = geneve_vport(vport);
59 	struct net *net = ovs_dp_get_net(vport->dp);
60 	__be16 dport = htons(geneve_port->port_no);
61 	__be16 sport = udp_flow_src_port(net, skb, 1, USHRT_MAX, true);
62 
63 	return ovs_tunnel_get_egress_info(upcall, ovs_dp_get_net(vport->dp),
64 					  skb, IPPROTO_UDP, sport, dport);
65 }
66 
67 static struct vport *geneve_tnl_create(const struct vport_parms *parms)
68 {
69 	struct net *net = ovs_dp_get_net(parms->dp);
70 	struct nlattr *options = parms->options;
71 	struct geneve_port *geneve_port;
72 	struct net_device *dev;
73 	struct vport *vport;
74 	struct nlattr *a;
75 	u16 dst_port;
76 	int err;
77 
78 	if (!options) {
79 		err = -EINVAL;
80 		goto error;
81 	}
82 
83 	a = nla_find_nested(options, OVS_TUNNEL_ATTR_DST_PORT);
84 	if (a && nla_len(a) == sizeof(u16)) {
85 		dst_port = nla_get_u16(a);
86 	} else {
87 		/* Require destination port from userspace. */
88 		err = -EINVAL;
89 		goto error;
90 	}
91 
92 	vport = ovs_vport_alloc(sizeof(struct geneve_port),
93 				&ovs_geneve_vport_ops, parms);
94 	if (IS_ERR(vport))
95 		return vport;
96 
97 	geneve_port = geneve_vport(vport);
98 	geneve_port->port_no = dst_port;
99 
100 	rtnl_lock();
101 	dev = geneve_dev_create_fb(net, parms->name, NET_NAME_USER, dst_port);
102 	if (IS_ERR(dev)) {
103 		rtnl_unlock();
104 		ovs_vport_free(vport);
105 		return ERR_CAST(dev);
106 	}
107 
108 	dev_change_flags(dev, dev->flags | IFF_UP);
109 	rtnl_unlock();
110 	return vport;
111 error:
112 	return ERR_PTR(err);
113 }
114 
115 static struct vport *geneve_create(const struct vport_parms *parms)
116 {
117 	struct vport *vport;
118 
119 	vport = geneve_tnl_create(parms);
120 	if (IS_ERR(vport))
121 		return vport;
122 
123 	return ovs_netdev_link(vport, parms->name);
124 }
125 
126 static struct vport_ops ovs_geneve_vport_ops = {
127 	.type		= OVS_VPORT_TYPE_GENEVE,
128 	.create		= geneve_create,
129 	.destroy	= ovs_netdev_tunnel_destroy,
130 	.get_options	= geneve_get_options,
131 	.send		= ovs_netdev_send,
132 	.owner          = THIS_MODULE,
133 	.get_egress_tun_info	= geneve_get_egress_tun_info,
134 };
135 
136 static int __init ovs_geneve_tnl_init(void)
137 {
138 	return ovs_vport_ops_register(&ovs_geneve_vport_ops);
139 }
140 
141 static void __exit ovs_geneve_tnl_exit(void)
142 {
143 	ovs_vport_ops_unregister(&ovs_geneve_vport_ops);
144 }
145 
146 module_init(ovs_geneve_tnl_init);
147 module_exit(ovs_geneve_tnl_exit);
148 
149 MODULE_DESCRIPTION("OVS: Geneve swiching port");
150 MODULE_LICENSE("GPL");
151 MODULE_ALIAS("vport-type-5");
152