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