netdev-dpdk: fix mbuf leaks
[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          /* XXX: No fragments support. */
130         match.wc.masks.nw_frag = FLOW_NW_FRAG_MASK;
131
132         /* 'udp_port' is zero for non-UDP tunnels (e.g. GRE). In this case it
133          * doesn't make sense to match on UDP port numbers. */
134         if (udp_port) {
135             match.wc.masks.tp_dst = OVS_BE16_MAX;
136         }
137         if (IN6_IS_ADDR_V4MAPPED(addr)) {
138             match.wc.masks.nw_dst = OVS_BE32_MAX;
139         } else {
140             match.wc.masks.ipv6_dst = in6addr_exact;
141         }
142         match.wc.masks.vlan_tci = OVS_BE16_MAX;
143         memset(&match.wc.masks.dl_dst, 0xff, sizeof (struct eth_addr));
144
145         cls_rule_init(&p->cr, &match, 0); /* Priority == 0. */
146         ovs_refcount_init(&p->ref_cnt);
147         ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
148
149         classifier_insert(&cls, &p->cr, CLS_MIN_VERSION, NULL, 0);
150     }
151 }
152
153 void
154 tnl_port_map_insert(odp_port_t port,
155                     ovs_be16 udp_port, const char dev_name[])
156 {
157     struct tnl_port *p;
158     struct ip_device *ip_dev;
159
160     ovs_mutex_lock(&mutex);
161     LIST_FOR_EACH(p, node, &port_list) {
162         if (udp_port == p->udp_port) {
163              goto out;
164         }
165     }
166
167     p = xzalloc(sizeof *p);
168     p->port = port;
169     p->udp_port = udp_port;
170     ovs_strlcpy(p->dev_name, dev_name, sizeof p->dev_name);
171     list_insert(&port_list, &p->node);
172
173     LIST_FOR_EACH(ip_dev, node, &addr_list) {
174         if (ip_dev->addr4 != INADDR_ANY) {
175             struct in6_addr addr4 = in6_addr_mapped_ipv4(ip_dev->addr4);
176             map_insert(p->port, ip_dev->mac, &addr4,
177                        p->udp_port, p->dev_name);
178         }
179         if (ipv6_addr_is_set(&ip_dev->addr6)) {
180             map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
181                        p->udp_port, p->dev_name);
182         }
183     }
184
185 out:
186     ovs_mutex_unlock(&mutex);
187 }
188
189 static void
190 tnl_port_unref(const struct cls_rule *cr)
191 {
192     struct tnl_port_in *p = tnl_port_cast(cr);
193
194     if (cr && ovs_refcount_unref_relaxed(&p->ref_cnt) == 1) {
195         if (classifier_remove(&cls, cr)) {
196             ovsrcu_postpone(tnl_port_free, p);
197         }
198     }
199 }
200
201 static void
202 map_delete(struct eth_addr mac, struct in6_addr *addr, ovs_be16 udp_port)
203 {
204     const struct cls_rule *cr;
205     struct flow flow;
206
207     tnl_port_init_flow(&flow, mac, addr, udp_port);
208
209     cr = classifier_lookup(&cls, CLS_MAX_VERSION, &flow, NULL);
210     tnl_port_unref(cr);
211 }
212
213 void
214 tnl_port_map_delete(ovs_be16 udp_port)
215 {
216     struct tnl_port *p, *next;
217     struct ip_device *ip_dev;
218     bool found = false;
219
220     ovs_mutex_lock(&mutex);
221     LIST_FOR_EACH_SAFE(p, next, node, &port_list) {
222         if (p->udp_port == udp_port) {
223             list_remove(&p->node);
224             found = true;
225             break;
226         }
227     }
228
229     if (!found) {
230         goto out;
231     }
232     LIST_FOR_EACH(ip_dev, node, &addr_list) {
233         if (ip_dev->addr4 != INADDR_ANY) {
234             struct in6_addr addr4 = in6_addr_mapped_ipv4(ip_dev->addr4);
235             map_delete(ip_dev->mac, &addr4, udp_port);
236         }
237         if (ipv6_addr_is_set(&ip_dev->addr6)) {
238             map_delete(ip_dev->mac, &ip_dev->addr6, udp_port);
239         }
240     }
241
242     free(p);
243 out:
244     ovs_mutex_unlock(&mutex);
245 }
246
247 /* 'flow' is non-const to allow for temporary modifications during the lookup.
248  * Any changes are restored before returning. */
249 odp_port_t
250 tnl_port_map_lookup(struct flow *flow, struct flow_wildcards *wc)
251 {
252     const struct cls_rule *cr = classifier_lookup(&cls, CLS_MAX_VERSION, flow,
253                                                   wc);
254
255     return (cr) ? tnl_port_cast(cr)->portno : ODPP_NONE;
256 }
257
258 static void
259 tnl_port_show_v(struct ds *ds)
260 {
261     const struct tnl_port_in *p;
262
263     CLS_FOR_EACH(p, cr, &cls) {
264         struct odputil_keybuf keybuf;
265         struct odputil_keybuf maskbuf;
266         struct flow flow;
267         const struct nlattr *key, *mask;
268         size_t key_len, mask_len;
269         struct flow_wildcards wc;
270         struct ofpbuf buf;
271         struct odp_flow_key_parms odp_parms = {
272             .flow = &flow,
273             .mask = &wc.masks,
274         };
275
276         ds_put_format(ds, "%s (%"PRIu32") : ", p->dev_name, p->portno);
277         minimask_expand(p->cr.match.mask, &wc);
278         miniflow_expand(p->cr.match.flow, &flow);
279
280         /* Key. */
281         odp_parms.odp_in_port = flow.in_port.odp_port;
282         odp_parms.support.recirc = true;
283         ofpbuf_use_stack(&buf, &keybuf, sizeof keybuf);
284         odp_flow_key_from_flow(&odp_parms, &buf);
285         key = buf.data;
286         key_len = buf.size;
287
288         /* mask*/
289         odp_parms.odp_in_port = wc.masks.in_port.odp_port;
290         odp_parms.support.recirc = false;
291         ofpbuf_use_stack(&buf, &maskbuf, sizeof maskbuf);
292         odp_flow_key_from_mask(&odp_parms, &buf);
293         mask = buf.data;
294         mask_len = buf.size;
295
296         /* build string. */
297         odp_flow_format(key, key_len, mask, mask_len, NULL, ds, false);
298         ds_put_format(ds, "\n");
299     }
300 }
301
302 static void
303 tnl_port_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
304                const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
305 {
306     struct ds ds = DS_EMPTY_INITIALIZER;
307     struct tnl_port *p;
308
309     ds_put_format(&ds, "Listening ports:\n");
310     ovs_mutex_lock(&mutex);
311     if (argc > 1) {
312         if (!strcasecmp(argv[1], "-v")) {
313             tnl_port_show_v(&ds);
314             goto out;
315         }
316     }
317
318     LIST_FOR_EACH(p, node, &port_list) {
319         ds_put_format(&ds, "%s (%"PRIu32")\n", p->dev_name, p->port);
320     }
321
322 out:
323     ovs_mutex_unlock(&mutex);
324     unixctl_command_reply(conn, ds_cstr(&ds));
325     ds_destroy(&ds);
326 }
327
328 static void
329 map_insert_ipdev(struct ip_device *ip_dev)
330 {
331     struct tnl_port *p;
332
333     LIST_FOR_EACH(p, node, &port_list) {
334         if (ip_dev->addr4 != INADDR_ANY) {
335             struct in6_addr addr4 = in6_addr_mapped_ipv4(ip_dev->addr4);
336             map_insert(p->port, ip_dev->mac, &addr4,
337                        p->udp_port, p->dev_name);
338         }
339         if (ipv6_addr_is_set(&ip_dev->addr6)) {
340             map_insert(p->port, ip_dev->mac, &ip_dev->addr6,
341                        p->udp_port, p->dev_name);
342         }
343     }
344 }
345
346 static void
347 insert_ipdev(const char dev_name[])
348 {
349     struct ip_device *ip_dev;
350     enum netdev_flags flags;
351     struct netdev *dev;
352     int error;
353     int error4, error6;
354
355     error = netdev_open(dev_name, NULL, &dev);
356     if (error) {
357         return;
358     }
359
360     error = netdev_get_flags(dev, &flags);
361     if (error || (flags & NETDEV_LOOPBACK)) {
362         netdev_close(dev);
363         return;
364     }
365
366     ip_dev = xzalloc(sizeof *ip_dev);
367     ip_dev->dev = dev;
368     ip_dev->change_seq = netdev_get_change_seq(dev);
369     error = netdev_get_etheraddr(ip_dev->dev, &ip_dev->mac);
370     if (error) {
371         free(ip_dev);
372         return;
373     }
374     error4 = netdev_get_in4(ip_dev->dev, (struct in_addr *)&ip_dev->addr4, NULL);
375     error6 = netdev_get_in6(ip_dev->dev, &ip_dev->addr6);
376     if (error4 && error6) {
377         free(ip_dev);
378         return;
379     }
380     ovs_strlcpy(ip_dev->dev_name, netdev_get_name(dev), sizeof ip_dev->dev_name);
381
382     list_insert(&addr_list, &ip_dev->node);
383     map_insert_ipdev(ip_dev);
384 }
385
386 static void
387 delete_ipdev(struct ip_device *ip_dev)
388 {
389     struct tnl_port *p;
390
391     LIST_FOR_EACH(p, node, &port_list) {
392         if (ip_dev->addr4 != INADDR_ANY) {
393             struct in6_addr addr4 = in6_addr_mapped_ipv4(ip_dev->addr4);
394             map_delete(ip_dev->mac, &addr4, p->udp_port);
395         }
396         if (ipv6_addr_is_set(&ip_dev->addr6)) {
397             map_delete(ip_dev->mac, &ip_dev->addr6, p->udp_port);
398         }
399     }
400
401     list_remove(&ip_dev->node);
402     netdev_close(ip_dev->dev);
403     free(ip_dev);
404 }
405
406 void
407 tnl_port_map_insert_ipdev(const char dev_name[])
408 {
409     struct ip_device *ip_dev, *next;
410
411     ovs_mutex_lock(&mutex);
412
413     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
414         if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
415             if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
416                 goto out;
417             }
418             /* Address changed. */
419             delete_ipdev(ip_dev);
420             break;
421         }
422     }
423     insert_ipdev(dev_name);
424
425 out:
426     ovs_mutex_unlock(&mutex);
427 }
428
429 void
430 tnl_port_map_delete_ipdev(const char dev_name[])
431 {
432     struct ip_device *ip_dev, *next;
433
434     ovs_mutex_lock(&mutex);
435     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
436         if (!strcmp(netdev_get_name(ip_dev->dev), dev_name)) {
437             delete_ipdev(ip_dev);
438         }
439     }
440     ovs_mutex_unlock(&mutex);
441 }
442
443 void
444 tnl_port_map_run(void)
445 {
446     struct ip_device *ip_dev, *next;
447
448     ovs_mutex_lock(&mutex);
449     LIST_FOR_EACH_SAFE(ip_dev, next, node, &addr_list) {
450         char dev_name[IFNAMSIZ];
451
452         if (ip_dev->change_seq == netdev_get_change_seq(ip_dev->dev)) {
453             continue;
454         }
455
456         /* Address changed. */
457         ovs_strlcpy(dev_name, ip_dev->dev_name, sizeof dev_name);
458         delete_ipdev(ip_dev);
459         insert_ipdev(dev_name);
460     }
461     ovs_mutex_unlock(&mutex);
462 }
463
464 void
465 tnl_port_map_init(void)
466 {
467     classifier_init(&cls, flow_segment_u64s);
468     list_init(&addr_list);
469     list_init(&port_list);
470     unixctl_command_register("tnl/ports/show", "-v", 0, 1, tnl_port_show, NULL);
471 }