packets: Introduce in6_addr_mapped_ipv4() and use where appropriate.
[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 = in6_addr_mapped_ipv4(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 = in6_addr_mapped_ipv4(ip_dev->addr4);
229             map_delete(ip_dev->mac, &addr4, udp_port);
230         }
231         if (ipv6_addr_is_set(&ip_dev->addr6)) {
232             map_delete(ip_dev->mac, &ip_dev->addr6, udp_port);
233         }
234     }
235
236     free(p);
237 out:
238     ovs_mutex_unlock(&mutex);
239 }
240
241 /* 'flow' is non-const to allow for temporary modifications during the lookup.
242  * Any changes are restored before returning. */
243 odp_port_t
244 tnl_port_map_lookup(struct flow *flow, struct flow_wildcards *wc)
245 {
246     const struct cls_rule *cr = classifier_lookup(&cls, CLS_MAX_VERSION, flow,
247                                                   wc);
248
249     return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
250 }
251
252 static void
253 tnl_port_show_v(struct ds *ds)
254 {
255     const struct tnl_port_in *p;
256
257     CLS_FOR_EACH(p, cr, &cls) {
258         struct odputil_keybuf keybuf;
259         struct odputil_keybuf maskbuf;
260         struct flow flow;
261         const struct nlattr *key, *mask;
262         size_t key_len, mask_len;
263         struct flow_wildcards wc;
264         struct ofpbuf buf;
265         struct odp_flow_key_parms odp_parms = {
266             .flow = &flow,
267             .mask = &wc.masks,
268         };
269
270         ds_put_format(ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
271         minimask_expand(p->cr.match.mask, &wc);
272         miniflow_expand(p->cr.match.flow, &flow);
273
274         /* Key. */
275         odp_parms.odp_in_port = flow.in_port.odp_port;
276         odp_parms.support.recirc = true;
277         ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
278         odp_flow_key_from_flow(&odp_parms, &buf);
279         key = buf.data;
280         key_len = buf.size;
281
282         /* mask*/
283         odp_parms.odp_in_port = wc.masks.in_port.odp_port;
284         odp_parms.support.recirc = false;
285         ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
286         odp_flow_key_from_mask(&odp_parms, &buf);
287         mask = buf.data;
288         mask_len = buf.size;
289
290         /* build string. */
291         odp_flow_format(key, key_len, mask, mask_len, NULL, ds, false);
292         ds_put_format(ds, "\n");
293     }
294 }
295
296 static void
297 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
298                const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
299 {
300     struct ds ds = DS_EMPTY_INITIALIZER;
301     struct tnl_port *p;
302
303     ds_put_format(&ds, "Listening ports:\n");
304     ovs_mutex_lock(&mutex);
305     if (argc > 1) {
306         if (!strcasecmp(argv[1], "-v")) {
307             tnl_port_show_v(&ds);
308             goto out;
309         }
310     }
311
312     LIST_FOR_EACH(p, node, &port_list) {
313         ds_put_format(&ds, "%s (%"PRIu32")\n", p->dev_name, p->port);
314     }
315
316 out:
317     ovs_mutex_unlock(&mutex);
318     unixctl_command_reply(conn, ds_cstr(&ds));
319     ds_destroy(&ds);
320 }
321
322 static void
323 map_insert_ipdev(struct ip_device *ip_dev)
324 {
325     struct tnl_port *p;
326
327     LIST_FOR_EACH(p, node, &port_list) {
328         if (ip_dev->addr4 != INADDR_ANY) {
329             struct in6_addr addr4 = in6_addr_mapped_ipv4(ip_dev->addr4);
330             map_insert(p->port, ip_dev->mac, &addr4,
331                        p->udp_port, p->dev_name);
332         }
333         if (ipv6_addr_is_set(&ip_dev->addr6)) {
334             map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
335                        p->udp_port, p->dev_name);
336         }
337     }
338 }
339
340 static void
341 insert_ipdev(const char dev_name[])
342 {
343     struct ip_device *ip_dev;
344     enum netdev_flags flags;
345     struct netdev *dev;
346     int error;
347     int error4, error6;
348
349     error = netdev_open(dev_name, NULL, &dev);
350     if (error) {
351         return;
352     }
353
354     error = netdev_get_flags(dev, &flags);
355     if (error || (flags & NETDEV_LOOPBACK)) {
356         netdev_close(dev);
357         return;
358     }
359
360     ip_dev = xzalloc(sizeof *ip_dev);
361     ip_dev->dev = dev;
362     ip_dev->change_seq = netdev_get_change_seq(dev);
363     error = netdev_get_etheraddr(ip_dev->dev, &ip_dev->mac);
364     if (error) {
365         free(ip_dev);
366         return;
367     }
368     error4 = netdev_get_in4(ip_dev->dev, (struct in_addr *)&ip_dev->addr4, NULL);
369     error6 = netdev_get_in6(ip_dev->dev, &ip_dev->addr6);
370     if (error4 && error6) {
371         free(ip_dev);
372         return;
373     }
374     ovs_strlcpy(ip_dev->dev_name, netdev_get_name(dev), sizeof ip_dev->dev_name);
375
376     list_insert(&addr_list, &ip_dev->node);
377     map_insert_ipdev(ip_dev);
378 }
379
380 static void
381 delete_ipdev(struct ip_device *ip_dev)
382 {
383     struct tnl_port *p;
384
385     LIST_FOR_EACH(p, node, &port_list) {
386         if (ip_dev->addr4 != INADDR_ANY) {
387             struct in6_addr addr4 = in6_addr_mapped_ipv4(ip_dev->addr4);
388             map_delete(ip_dev->mac, &addr4, p->udp_port);
389         }
390         if (ipv6_addr_is_set(&ip_dev->addr6)) {
391             map_delete(ip_dev->mac, &ip_dev->addr6, p->udp_port);
392         }
393     }
394
395     list_remove(&ip_dev->node);
396     netdev_close(ip_dev->dev);
397     free(ip_dev);
398 }
399
400 void
401 tnl_port_map_insert_ipdev(const char dev_name[])
402 {
403     struct ip_device *ip_dev, *next;
404
405     ovs_mutex_lock(&mutex);
406
407     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
408         if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
409             if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
410                 goto out;
411             }
412             /* Address changed. */
413             delete_ipdev(ip_dev);
414             break;
415         }
416     }
417     insert_ipdev(dev_name);
418
419 out:
420     ovs_mutex_unlock(&mutex);
421 }
422
423 void
424 tnl_port_map_delete_ipdev(const char dev_name[])
425 {
426     struct ip_device *ip_dev, *next;
427
428     ovs_mutex_lock(&mutex);
429     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
430         if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
431             delete_ipdev(ip_dev);
432         }
433     }
434     ovs_mutex_unlock(&mutex);
435 }
436
437 void
438 tnl_port_map_run(void)
439 {
440     struct ip_device *ip_dev, *next;
441
442     ovs_mutex_lock(&mutex);
443     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
444         char dev_name[IFNAMSIZ];
445
446         if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
447             continue;
448         }
449
450         /* Address changed. */
451         ovs_strlcpy(dev_name, ip_dev->dev_name, sizeof dev_name);
452         delete_ipdev(ip_dev);
453         insert_ipdev(dev_name);
454     }
455     ovs_mutex_unlock(&mutex);
456 }
457
458 void
459 tnl_port_map_init(void)
460 {
461     classifier_init(&cls, flow_segment_u64s);
462     list_init(&addr_list);
463     list_init(&port_list);
464     unixctl_command_register("tnl/ports/show", "-v", 0, 1, tnl_port_show, NULL);
465 }