]> git.proxmox.com Git - mirror_ovs.git/blob - lib/tnl-ports.c
userspace: Add GTP-U support.
[mirror_ovs.git] / lib / tnl-ports.c
1 /*
2 * Copyright (c) 2014, 2015, 2017 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18
19 #include "tnl-ports.h"
20
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24
25 #include "classifier.h"
26 #include "openvswitch/dynamic-string.h"
27 #include "hash.h"
28 #include "openvswitch/list.h"
29 #include "netdev.h"
30 #include "openvswitch/ofpbuf.h"
31 #include "ovs-thread.h"
32 #include "odp-util.h"
33 #include "ovs-thread.h"
34 #include "unixctl.h"
35 #include "util.h"
36
37 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
38 static struct classifier cls; /* Tunnel ports. */
39
40 struct ip_device {
41 struct netdev *dev;
42 struct eth_addr mac;
43 struct in6_addr *addr;
44 int n_addr;
45 uint64_t change_seq;
46 struct ovs_list node;
47 char dev_name[IFNAMSIZ];
48 };
49
50 static struct ovs_list addr_list;
51
52 struct tnl_port {
53 odp_port_t port;
54 struct ovs_refcount ref_cnt;
55 ovs_be16 tp_port;
56 uint8_t nw_proto;
57 char dev_name[IFNAMSIZ];
58 struct ovs_list node;
59 };
60
61 static struct ovs_list port_list;
62
63 struct tnl_port_in {
64 struct cls_rule cr;
65 odp_port_t portno;
66 struct ovs_refcount ref_cnt;
67 char dev_name[IFNAMSIZ];
68 };
69
70 static struct tnl_port_in *
71 tnl_port_cast(const struct cls_rule *cr)
72 {
73 BUILD_ASSERT_DECL(offsetof(struct tnl_port_in, cr) == 0);
74
75 return CONTAINER_OF(cr, struct tnl_port_in, cr);
76 }
77
78 static void
79 tnl_port_free(struct tnl_port_in *p)
80 {
81 cls_rule_destroy(&p->cr);
82 free(p);
83 }
84
85 static void
86 tnl_port_init_flow(struct flow *flow, struct eth_addr mac,
87 struct in6_addr *addr, uint8_t nw_proto, ovs_be16 tp_port)
88 {
89 memset(flow, 0, sizeof *flow);
90
91 flow->dl_dst = mac;
92 if (IN6_IS_ADDR_V4MAPPED(addr)) {
93 flow->dl_type = htons(ETH_TYPE_IP);
94 flow->nw_dst = in6_addr_get_mapped_ipv4(addr);
95 } else {
96 flow->dl_type = htons(ETH_TYPE_IPV6);
97 flow->ipv6_dst = *addr;
98 }
99
100 flow->nw_proto = nw_proto;
101 flow->tp_dst = tp_port;
102 }
103
104 static void
105 map_insert(odp_port_t port, struct eth_addr mac, struct in6_addr *addr,
106 uint8_t nw_proto, ovs_be16 tp_port, const char dev_name[])
107 {
108 const struct cls_rule *cr;
109 struct tnl_port_in *p;
110 struct match match;
111
112 memset(&match, 0, sizeof match);
113 tnl_port_init_flow(&match.flow, mac, addr, nw_proto, tp_port);
114
115 do {
116 cr = classifier_lookup(&cls, OVS_VERSION_MAX, &match.flow, NULL);
117 p = tnl_port_cast(cr);
118 /* Try again if the rule was released before we get the reference. */
119 } while (p && !ovs_refcount_try_ref_rcu(&p->ref_cnt));
120
121 if (!p) {
122 p = xzalloc(sizeof *p);
123 p->portno = port;
124
125 match.wc.masks.dl_type = OVS_BE16_MAX;
126 match.wc.masks.nw_proto = 0xff;
127 /* XXX: No fragments support. */
128 match.wc.masks.nw_frag = FLOW_NW_FRAG_MASK;
129
130 /* 'tp_port' is zero for GRE tunnels. In this case it
131 * doesn't make sense to match on UDP port numbers. */
132 if (tp_port) {
133 match.wc.masks.tp_dst = OVS_BE16_MAX;
134 }
135 if (IN6_IS_ADDR_V4MAPPED(addr)) {
136 match.wc.masks.nw_dst = OVS_BE32_MAX;
137 } else {
138 match.wc.masks.ipv6_dst = in6addr_exact;
139 }
140 match.wc.masks.vlans[0].tci = OVS_BE16_MAX;
141 memset(&match.wc.masks.dl_dst, 0xff, sizeof (struct eth_addr));
142
143 cls_rule_init(&p->cr, &match, 0); /* Priority == 0. */
144 ovs_refcount_init(&p->ref_cnt);
145 ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
146
147 classifier_insert(&cls, &p->cr, OVS_VERSION_MIN, NULL, 0);
148 }
149 }
150
151 static void
152 map_insert_ipdev__(struct ip_device *ip_dev, char dev_name[],
153 odp_port_t port, uint8_t nw_proto, ovs_be16 tp_port)
154 {
155 if (ip_dev->n_addr) {
156 int i;
157
158 for (i = 0; i < ip_dev->n_addr; i++) {
159 map_insert(port, ip_dev->mac, &ip_dev->addr[i],
160 nw_proto, tp_port, dev_name);
161 }
162 }
163 }
164
165 static uint8_t
166 tnl_type_to_nw_proto(const char type[])
167 {
168 if (!strcmp(type, "geneve")) {
169 return IPPROTO_UDP;
170 }
171 if (!strcmp(type, "stt")) {
172 return IPPROTO_TCP;
173 }
174 if (!strcmp(type, "gre") || !strcmp(type, "erspan") ||
175 !strcmp(type, "ip6erspan") || !strcmp(type, "ip6gre")) {
176 return IPPROTO_GRE;
177 }
178 if (!strcmp(type, "vxlan")) {
179 return IPPROTO_UDP;
180 }
181 if (!strcmp(type, "gtpu")) {
182 return IPPROTO_UDP;
183 }
184 return 0;
185 }
186
187 void
188 tnl_port_map_insert(odp_port_t port, ovs_be16 tp_port,
189 const char dev_name[], const char type[])
190 {
191 struct tnl_port *p;
192 struct ip_device *ip_dev;
193 uint8_t nw_proto;
194
195 nw_proto = tnl_type_to_nw_proto(type);
196 if (!nw_proto) {
197 return;
198 }
199
200 ovs_mutex_lock(&mutex);
201 LIST_FOR_EACH(p, node, &port_list) {
202 if (p->port == port && p->nw_proto == nw_proto) {
203 ovs_refcount_ref(&p->ref_cnt);
204 goto out;
205 }
206 }
207
208 p = xzalloc(sizeof *p);
209 p->port = port;
210 p->tp_port = tp_port;
211 p->nw_proto = nw_proto;
212 ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
213 ovs_refcount_init(&p->ref_cnt);
214 ovs_list_insert(&port_list, &p->node);
215
216 LIST_FOR_EACH(ip_dev, node, &addr_list) {
217 map_insert_ipdev__(ip_dev, p->dev_name, p->port, p->nw_proto, p->tp_port);
218 }
219
220 out:
221 ovs_mutex_unlock(&mutex);
222 }
223
224 static void
225 tnl_port_unref(const struct cls_rule *cr)
226 {
227 struct tnl_port_in *p = tnl_port_cast(cr);
228
229 if (cr && ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
230 classifier_remove_assert(&cls, cr);
231 ovsrcu_postpone(tnl_port_free, p);
232 }
233 }
234
235 static void
236 map_delete(struct eth_addr mac, struct in6_addr *addr,
237 ovs_be16 tp_port, uint8_t nw_proto)
238 {
239 const struct cls_rule *cr;
240 struct flow flow;
241
242 tnl_port_init_flow(&flow, mac, addr, nw_proto, tp_port);
243
244 cr = classifier_lookup(&cls, OVS_VERSION_MAX, &flow, NULL);
245 tnl_port_unref(cr);
246 }
247
248 static void
249 ipdev_map_delete(struct ip_device *ip_dev, ovs_be16 tp_port, uint8_t nw_proto)
250 {
251 if (ip_dev->n_addr) {
252 int i;
253
254 for (i = 0; i < ip_dev->n_addr; i++) {
255 map_delete(ip_dev->mac, &ip_dev->addr[i], tp_port, nw_proto);
256 }
257 }
258 }
259
260 void
261 tnl_port_map_delete(odp_port_t port, const char type[])
262 {
263 struct tnl_port *p, *next;
264 struct ip_device *ip_dev;
265 uint8_t nw_proto;
266
267 nw_proto = tnl_type_to_nw_proto(type);
268
269 ovs_mutex_lock(&mutex);
270 LIST_FOR_EACH_SAFE(p, next, node, &port_list) {
271 if (p->port == port && p->nw_proto == nw_proto &&
272 ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
273 ovs_list_remove(&p->node);
274 LIST_FOR_EACH(ip_dev, node, &addr_list) {
275 ipdev_map_delete(ip_dev, p->tp_port, p->nw_proto);
276 }
277 free(p);
278 break;
279 }
280 }
281 ovs_mutex_unlock(&mutex);
282 }
283
284 /* 'flow' is non-const to allow for temporary modifications during the lookup.
285 * Any changes are restored before returning. */
286 odp_port_t
287 tnl_port_map_lookup(struct flow *flow, struct flow_wildcards *wc)
288 {
289 const struct cls_rule *cr = classifier_lookup(&cls, OVS_VERSION_MAX, flow,
290 wc);
291
292 return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
293 }
294
295 static void
296 tnl_port_show_v(struct ds *ds)
297 {
298 const struct tnl_port_in *p;
299
300 CLS_FOR_EACH(p, cr, &cls) {
301 struct odputil_keybuf keybuf;
302 struct odputil_keybuf maskbuf;
303 struct flow flow;
304 const struct nlattr *key, *mask;
305 size_t key_len, mask_len;
306 struct flow_wildcards wc;
307 struct ofpbuf buf;
308 struct odp_flow_key_parms odp_parms = {
309 .flow = &flow,
310 .mask = &wc.masks,
311 };
312
313 ds_put_format(ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
314 minimask_expand(p->cr.match.mask, &wc);
315 miniflow_expand(p->cr.match.flow, &flow);
316
317 /* Key. */
318 odp_parms.support.recirc = true;
319 ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
320 odp_flow_key_from_flow(&odp_parms, &buf);
321 key = buf.data;
322 key_len = buf.size;
323
324 /* mask*/
325 odp_parms.support.recirc = false;
326 ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
327 odp_flow_key_from_mask(&odp_parms, &buf);
328 mask = buf.data;
329 mask_len = buf.size;
330
331 /* build string. */
332 odp_flow_format(key, key_len, mask, mask_len, NULL, ds, false);
333 ds_put_format(ds, "\n");
334 }
335 }
336
337 static void
338 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
339 const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
340 {
341 struct ds ds = DS_EMPTY_INITIALIZER;
342 struct tnl_port *p;
343
344 ds_put_format(&ds, "Listening ports:\n");
345 ovs_mutex_lock(&mutex);
346 if (argc > 1) {
347 if (!strcasecmp(argv[1], "-v")) {
348 tnl_port_show_v(&ds);
349 goto out;
350 }
351 }
352
353 LIST_FOR_EACH(p, node, &port_list) {
354 ds_put_format(&ds, "%s (%"PRIu32") ref_cnt=%u\n", p->dev_name, p->port,
355 ovs_refcount_read(&p->ref_cnt));
356 }
357
358 out:
359 ovs_mutex_unlock(&mutex);
360 unixctl_command_reply(conn, ds_cstr(&ds));
361 ds_destroy(&ds);
362 }
363
364 static void
365 map_insert_ipdev(struct ip_device *ip_dev)
366 {
367 struct tnl_port *p;
368
369 LIST_FOR_EACH(p, node, &port_list) {
370 map_insert_ipdev__(ip_dev, p->dev_name, p->port, p->nw_proto, p->tp_port);
371 }
372 }
373
374 static void
375 insert_ipdev__(struct netdev *dev,
376 struct in6_addr *addr, int n_addr)
377 {
378 struct ip_device *ip_dev;
379 enum netdev_flags flags;
380 int error;
381
382 error = netdev_get_flags(dev, &flags);
383 if (error || (flags & NETDEV_LOOPBACK)) {
384 goto err;
385 }
386
387 ip_dev = xzalloc(sizeof *ip_dev);
388 ip_dev->dev = netdev_ref(dev);
389 ip_dev->change_seq = netdev_get_change_seq(dev);
390 error = netdev_get_etheraddr(ip_dev->dev, &ip_dev->mac);
391 if (error) {
392 goto err_free_ipdev;
393 }
394 ip_dev->addr = addr;
395 ip_dev->n_addr = n_addr;
396 ovs_strlcpy(ip_dev->dev_name, netdev_get_name(dev), sizeof ip_dev->dev_name);
397 ovs_list_insert(&addr_list, &ip_dev->node);
398 map_insert_ipdev(ip_dev);
399 return;
400
401 err_free_ipdev:
402 netdev_close(ip_dev->dev);
403 free(ip_dev);
404 err:
405 free(addr);
406 }
407
408 static void
409 insert_ipdev(const char dev_name[])
410 {
411 struct in6_addr *addr, *mask;
412 struct netdev *dev;
413 int error, n_in6;
414
415 error = netdev_open(dev_name, netdev_get_type_from_name(dev_name), &dev);
416 if (error) {
417 return;
418 }
419
420 error = netdev_get_addr_list(dev, &addr, &mask, &n_in6);
421 if (error) {
422 netdev_close(dev);
423 return;
424 }
425 free(mask);
426 insert_ipdev__(dev, addr, n_in6);
427 netdev_close(dev);
428 }
429
430 static void
431 delete_ipdev(struct ip_device *ip_dev)
432 {
433 struct tnl_port *p;
434
435 LIST_FOR_EACH(p, node, &port_list) {
436 ipdev_map_delete(ip_dev, p->tp_port, p->nw_proto);
437 }
438
439 ovs_list_remove(&ip_dev->node);
440 netdev_close(ip_dev->dev);
441 free(ip_dev->addr);
442 free(ip_dev);
443 }
444
445 void
446 tnl_port_map_insert_ipdev(const char dev_name[])
447 {
448 struct ip_device *ip_dev, *next;
449
450 ovs_mutex_lock(&mutex);
451
452 LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
453 if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
454 if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
455 goto out;
456 }
457 /* Address changed. */
458 delete_ipdev(ip_dev);
459 }
460 }
461 insert_ipdev(dev_name);
462
463 out:
464 ovs_mutex_unlock(&mutex);
465 }
466
467 void
468 tnl_port_map_delete_ipdev(const char dev_name[])
469 {
470 struct ip_device *ip_dev, *next;
471
472 ovs_mutex_lock(&mutex);
473 LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
474 if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
475 delete_ipdev(ip_dev);
476 }
477 }
478 ovs_mutex_unlock(&mutex);
479 }
480
481 void
482 tnl_port_map_run(void)
483 {
484 struct ip_device *ip_dev, *next;
485
486 ovs_mutex_lock(&mutex);
487 LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
488 char dev_name[IFNAMSIZ];
489
490 if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
491 continue;
492 }
493
494 /* Address changed. */
495 ovs_strlcpy_arrays(dev_name, ip_dev->dev_name);
496 delete_ipdev(ip_dev);
497 insert_ipdev(dev_name);
498 }
499 ovs_mutex_unlock(&mutex);
500 }
501
502 void
503 tnl_port_map_init(void)
504 {
505 classifier_init(&cls, flow_segment_u64s);
506 ovs_list_init(&addr_list);
507 ovs_list_init(&port_list);
508 unixctl_command_register("tnl/ports/show", "-v", 0, 1, tnl_port_show, NULL);
509 }