77cf5bbbd38dcda2c9bf408b791b58420e439923
[cascardo/ovs.git] / lib / tnl-ports.c
1 /*
2  * Copyright (c) 2014, 2015 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
24 #include "classifier.h"
25 #include "dynamic-string.h"
26 #include "hash.h"
27 #include "list.h"
28 #include "ofpbuf.h"
29 #include "ovs-thread.h"
30 #include "odp-util.h"
31 #include "tnl-arp-cache.h"
32 #include "ovs-thread.h"
33 #include "unixctl.h"
34 #include "util.h"
35
36 static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
37 static struct classifier cls;   /* Tunnel ports. */
38
39 struct ip_device {
40     struct netdev *dev;
41     struct eth_addr mac;
42     ovs_be32 addr4;
43     struct in6_addr addr6;
44     uint64_t change_seq;
45     struct ovs_list node;
46     char dev_name[IFNAMSIZ];
47 };
48
49 static struct ovs_list addr_list;
50
51 struct tnl_port {
52     odp_port_t port;
53     ovs_be16 udp_port;
54     char dev_name[IFNAMSIZ];
55     struct ovs_list node;
56 };
57
58 static struct ovs_list port_list;
59
60 struct tnl_port_in {
61     struct cls_rule cr;
62     odp_port_t portno;
63     struct ovs_refcount ref_cnt;
64     char dev_name[IFNAMSIZ];
65 };
66
67 static struct tnl_port_in *
68 tnl_port_cast(const struct cls_rule *cr)
69 {
70     BUILD_ASSERT_DECL(offsetof(struct tnl_port_in, cr) == 0);
71
72     return CONTAINER_OF(cr, struct tnl_port_in, cr);
73 }
74
75 static void
76 tnl_port_free(struct tnl_port_in *p)
77 {
78     cls_rule_destroy(&p->cr);
79     free(p);
80 }
81
82 static void
83 tnl_port_init_flow(struct flow *flow, struct eth_addr mac,
84                    struct in6_addr *addr, ovs_be16 udp_port)
85 {
86     memset(flow, 0, sizeof *flow);
87
88     flow->dl_dst = mac;
89     if (IN6_IS_ADDR_V4MAPPED(addr)) {
90         flow->dl_type = htons(ETH_TYPE_IP);
91         flow->nw_dst = in6_addr_get_mapped_ipv4(addr);
92     } else {
93         flow->dl_type = htons(ETH_TYPE_IPV6);
94         flow->ipv6_dst = *addr;
95     }
96
97     if (udp_port) {
98         flow->nw_proto = IPPROTO_UDP;
99     } else {
100         flow->nw_proto = IPPROTO_GRE;
101     }
102     flow->tp_dst = udp_port;
103 }
104
105 static void
106 map_insert(odp_port_t port, struct eth_addr mac, struct in6_addr *addr,
107            ovs_be16 udp_port, const char dev_name[])
108 {
109     const struct cls_rule *cr;
110     struct tnl_port_in *p;
111     struct match match;
112
113     memset(&match, 0, sizeof match);
114     tnl_port_init_flow(&match.flow, mac, addr, udp_port);
115
116     do {
117         cr = classifier_lookup(&cls, CLS_MAX_VERSION, &match.flow, NULL);
118         p = tnl_port_cast(cr);
119         /* Try again if the rule was released before we get the reference. */
120     } while (p && !ovs_refcount_try_ref_rcu(&p->ref_cnt));
121
122     if (!p) {
123         p = xzalloc(sizeof *p);
124         p->portno = port;
125
126         match.wc.masks.dl_type = OVS_BE16_MAX;
127         match.wc.masks.nw_proto = 0xff;
128         match.wc.masks.nw_frag = 0xff;      /* XXX: No fragments support. */
129         match.wc.masks.tp_dst = OVS_BE16_MAX;
130         if (IN6_IS_ADDR_V4MAPPED(addr)) {
131             match.wc.masks.nw_dst = OVS_BE32_MAX;
132         } else {
133             match.wc.masks.ipv6_dst = in6addr_exact;
134         }
135         match.wc.masks.vlan_tci = OVS_BE16_MAX;
136         memset(&match.wc.masks.dl_dst, 0xff, sizeof (struct eth_addr));
137
138         cls_rule_init(&p->cr, &match, 0); /* Priority == 0. */
139         ovs_refcount_init(&p->ref_cnt);
140         ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
141
142         classifier_insert(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
143     }
144 }
145
146 void
147 tnl_port_map_insert(odp_port_t port,
148                     ovs_be16 udp_port, const char dev_name[])
149 {
150     struct tnl_port *p;
151     struct ip_device *ip_dev;
152
153     ovs_mutex_lock(&mutex);
154     LIST_FOR_EACH(p, node, &port_list) {
155         if (udp_port == p->udp_port) {
156              goto out;
157         }
158     }
159
160     p = xzalloc(sizeof *p);
161     p->port = port;
162     p->udp_port = udp_port;
163     ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
164     list_insert(&port_list, &p->node);
165
166     LIST_FOR_EACH(ip_dev, node, &addr_list) {
167         if (ip_dev->addr4 != INADDR_ANY) {
168             struct in6_addr addr4;
169             in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
170             map_insert(p->port, ip_dev->mac, &addr4,
171                        p->udp_port, p->dev_name);
172         }
173         if (ipv6_addr_is_set(&ip_dev->addr6)) {
174             map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
175                        p->udp_port, p->dev_name);
176         }
177     }
178
179 out:
180     ovs_mutex_unlock(&mutex);
181 }
182
183 static void
184 tnl_port_unref(const struct cls_rule *cr)
185 {
186     struct tnl_port_in *p = tnl_port_cast(cr);
187
188     if (cr && ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
189         if (classifier_remove(&cls, cr)) {
190             ovsrcu_postpone(tnl_port_free, p);
191         }
192     }
193 }
194
195 static void
196 map_delete(struct eth_addr mac, struct in6_addr *addr, ovs_be16 udp_port)
197 {
198     const struct cls_rule *cr;
199     struct flow flow;
200
201     tnl_port_init_flow(&flow, mac, addr, udp_port);
202
203     cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
204     tnl_port_unref(cr);
205 }
206
207 void
208 tnl_port_map_delete(ovs_be16 udp_port)
209 {
210     struct tnl_port *p, *next;
211     struct ip_device *ip_dev;
212     bool found = false;
213
214     ovs_mutex_lock(&mutex);
215     LIST_FOR_EACH_SAFE(p, next, node, &port_list) {
216         if (p->udp_port == udp_port) {
217             list_remove(&p->node);
218             found = true;
219             break;
220         }
221     }
222
223     if (!found) {
224         goto out;
225     }
226     LIST_FOR_EACH(ip_dev, node, &addr_list) {
227         if (ip_dev->addr4 != INADDR_ANY) {
228             struct in6_addr addr4;
229             in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
230             map_delete(ip_dev->mac, &addr4, udp_port);
231         }
232         if (ipv6_addr_is_set(&ip_dev->addr6)) {
233             map_delete(ip_dev->mac, &ip_dev->addr6, udp_port);
234         }
235     }
236
237     free(p);
238 out:
239     ovs_mutex_unlock(&mutex);
240 }
241
242 /* 'flow' is non-const to allow for temporary modifications during the lookup.
243  * Any changes are restored before returning. */
244 odp_port_t
245 tnl_port_map_lookup(struct flow *flow, struct flow_wildcards *wc)
246 {
247     const struct cls_rule *cr = classifier_lookup(&cls, CLS_MAX_VERSION, flow,
248                                                   wc);
249
250     return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
251 }
252
253 static void
254 tnl_port_show_v(struct ds *ds)
255 {
256     const struct tnl_port_in *p;
257
258     CLS_FOR_EACH(p, cr, &cls) {
259         struct odputil_keybuf keybuf;
260         struct odputil_keybuf maskbuf;
261         struct flow flow;
262         const struct nlattr *key, *mask;
263         size_t key_len, mask_len;
264         struct flow_wildcards wc;
265         struct ofpbuf buf;
266         struct odp_flow_key_parms odp_parms = {
267             .flow = &flow,
268             .mask = &wc.masks,
269         };
270
271         ds_put_format(ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
272         minimask_expand(p->cr.match.mask, &wc);
273         miniflow_expand(p->cr.match.flow, &flow);
274
275         /* Key. */
276         odp_parms.odp_in_port = flow.in_port.odp_port;
277         odp_parms.support.recirc = true;
278         ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
279         odp_flow_key_from_flow(&odp_parms, &buf);
280         key = buf.data;
281         key_len = buf.size;
282
283         /* mask*/
284         odp_parms.odp_in_port = wc.masks.in_port.odp_port;
285         odp_parms.support.recirc = false;
286         ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
287         odp_flow_key_from_mask(&odp_parms, &buf);
288         mask = buf.data;
289         mask_len = buf.size;
290
291         /* build string. */
292         odp_flow_format(key, key_len, mask, mask_len, NULL, ds, false);
293         ds_put_format(ds, "\n");
294     }
295 }
296
297 static void
298 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
299                const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
300 {
301     struct ds ds = DS_EMPTY_INITIALIZER;
302     struct tnl_port *p;
303
304     ds_put_format(&ds, "Listening ports:\n");
305     ovs_mutex_lock(&mutex);
306     if (argc > 1) {
307         if (!strcasecmp(argv[1], "-v")) {
308             tnl_port_show_v(&ds);
309             goto out;
310         }
311     }
312
313     LIST_FOR_EACH(p, node, &port_list) {
314         ds_put_format(&ds, "%s (%"PRIu32")\n", p->dev_name, p->port);
315     }
316
317 out:
318     ovs_mutex_unlock(&mutex);
319     unixctl_command_reply(conn, ds_cstr(&ds));
320     ds_destroy(&ds);
321 }
322
323 static void
324 map_insert_ipdev(struct ip_device *ip_dev)
325 {
326     struct tnl_port *p;
327
328     LIST_FOR_EACH(p, node, &port_list) {
329         if (ip_dev->addr4 != INADDR_ANY) {
330             struct in6_addr addr4;
331             in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
332             map_insert(p->port, ip_dev->mac, &addr4,
333                        p->udp_port, p->dev_name);
334         }
335         if (ipv6_addr_is_set(&ip_dev->addr6)) {
336             map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
337                        p->udp_port, p->dev_name);
338         }
339     }
340 }
341
342 static void
343 insert_ipdev(const char dev_name[])
344 {
345     struct ip_device *ip_dev;
346     enum netdev_flags flags;
347     struct netdev *dev;
348     int error;
349     int error4, error6;
350
351     error = netdev_open(dev_name, NULL, &dev);
352     if (error) {
353         return;
354     }
355
356     error = netdev_get_flags(dev, &flags);
357     if (error || (flags & NETDEV_LOOPBACK)) {
358         netdev_close(dev);
359         return;
360     }
361
362     ip_dev = xzalloc(sizeof *ip_dev);
363     ip_dev->dev = dev;
364     ip_dev->change_seq = netdev_get_change_seq(dev);
365     error = netdev_get_etheraddr(ip_dev->dev, &ip_dev->mac);
366     if (error) {
367         free(ip_dev);
368         return;
369     }
370     error4 = netdev_get_in4(ip_dev->dev, (struct in_addr *)&ip_dev->addr4, NULL);
371     error6 = netdev_get_in6(ip_dev->dev, &ip_dev->addr6);
372     if (error4 && error6) {
373         free(ip_dev);
374         return;
375     }
376     ovs_strlcpy(ip_dev->dev_name, netdev_get_name(dev), sizeof ip_dev->dev_name);
377
378     list_insert(&addr_list, &ip_dev->node);
379     map_insert_ipdev(ip_dev);
380 }
381
382 static void
383 delete_ipdev(struct ip_device *ip_dev)
384 {
385     struct tnl_port *p;
386
387     LIST_FOR_EACH(p, node, &port_list) {
388         if (ip_dev->addr4 != INADDR_ANY) {
389             struct in6_addr addr4;
390             in6_addr_set_mapped_ipv4(&addr4, ip_dev->addr4);
391             map_delete(ip_dev->mac, &addr4, p->udp_port);
392         }
393         if (ipv6_addr_is_set(&ip_dev->addr6)) {
394             map_delete(ip_dev->mac, &ip_dev->addr6, p->udp_port);
395         }
396     }
397
398     list_remove(&ip_dev->node);
399     netdev_close(ip_dev->dev);
400     free(ip_dev);
401 }
402
403 void
404 tnl_port_map_insert_ipdev(const char dev_name[])
405 {
406     struct ip_device *ip_dev, *next;
407
408     ovs_mutex_lock(&mutex);
409
410     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
411         if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
412             if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
413                 goto out;
414             }
415             /* Address changed. */
416             delete_ipdev(ip_dev);
417             break;
418         }
419     }
420     insert_ipdev(dev_name);
421
422 out:
423     ovs_mutex_unlock(&mutex);
424 }
425
426 void
427 tnl_port_map_delete_ipdev(const char dev_name[])
428 {
429     struct ip_device *ip_dev, *next;
430
431     ovs_mutex_lock(&mutex);
432     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
433         if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
434             delete_ipdev(ip_dev);
435         }
436     }
437     ovs_mutex_unlock(&mutex);
438 }
439
440 void
441 tnl_port_map_run(void)
442 {
443     struct ip_device *ip_dev, *next;
444
445     ovs_mutex_lock(&mutex);
446     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
447         char dev_name[IFNAMSIZ];
448
449         if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
450             continue;
451         }
452
453         /* Address changed. */
454         ovs_strlcpy(dev_name, ip_dev->dev_name, sizeof dev_name);
455         delete_ipdev(ip_dev);
456         insert_ipdev(dev_name);
457     }
458     ovs_mutex_unlock(&mutex);
459 }
460
461 void
462 tnl_port_map_init(void)
463 {
464     classifier_init(&cls, flow_segment_u64s);
465     list_init(&addr_list);
466     list_init(&port_list);
467     unixctl_command_register("tnl/ports/show", "-v", 0, 1, tnl_port_show, NULL);
468 }