netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / ofproto / ofproto-dpif-mirror.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2015 Nicira, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License. */
14
15 #include <config.h>
16
17 #include "ofproto-dpif-mirror.h"
18
19 #include <errno.h>
20
21 #include "hmap.h"
22 #include "hmapx.h"
23 #include "ofproto.h"
24 #include "vlan-bitmap.h"
25 #include "openvswitch/vlog.h"
26
27 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_mirror);
28
29 #define MIRROR_MASK_C(X) UINT32_C(X)
30 BUILD_ASSERT_DECL(sizeof(mirror_mask_t) * CHAR_BIT >= MAX_MIRRORS);
31
32 struct mbridge {
33     struct mirror *mirrors[MAX_MIRRORS];
34     struct hmap mbundles;
35
36     bool need_revalidate;
37     bool has_mirrors;
38
39     struct ovs_refcount ref_cnt;
40 };
41
42 struct mbundle {
43     struct hmap_node hmap_node; /* In parent 'mbridge' map. */
44     struct ofbundle *ofbundle;
45
46     mirror_mask_t src_mirrors;  /* Mirrors triggered when packet received. */
47     mirror_mask_t dst_mirrors;  /* Mirrors triggered when packet sent. */
48     mirror_mask_t mirror_out;   /* Mirrors that output to this mbundle. */
49 };
50
51 struct mirror {
52     struct mbridge *mbridge;    /* Owning ofproto. */
53     size_t idx;                 /* In ofproto's "mirrors" array. */
54     void *aux;                  /* Key supplied by ofproto's client. */
55
56     /* Selection criteria. */
57     struct hmapx srcs;          /* Contains "struct mbundle*"s. */
58     struct hmapx dsts;          /* Contains "struct mbundle*"s. */
59     unsigned long *vlans;       /* Bitmap of chosen VLANs, NULL selects all. */
60
61     /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
62     struct mbundle *out;        /* Output port or NULL. */
63     int out_vlan;               /* Output VLAN or -1. */
64     mirror_mask_t dup_mirrors;  /* Bitmap of mirrors with the same output. */
65
66     /* Counters. */
67     int64_t packet_count;       /* Number of packets sent. */
68     int64_t byte_count;         /* Number of bytes sent. */
69 };
70
71 static struct mirror *mirror_lookup(struct mbridge *, void *aux);
72 static struct mbundle *mbundle_lookup(const struct mbridge *,
73                                       struct ofbundle *);
74 static void mbundle_lookup_multiple(const struct mbridge *, struct ofbundle **,
75                                   size_t n_bundles, struct hmapx *mbundles);
76 static int mirror_scan(struct mbridge *);
77 static void mirror_update_dups(struct mbridge *);
78
79 struct mbridge *
80 mbridge_create(void)
81 {
82     struct mbridge *mbridge;
83
84     mbridge = xzalloc(sizeof *mbridge);
85     ovs_refcount_init(&mbridge->ref_cnt);
86
87     hmap_init(&mbridge->mbundles);
88     return mbridge;
89 }
90
91 struct mbridge *
92 mbridge_ref(const struct mbridge *mbridge_)
93 {
94     struct mbridge *mbridge = CONST_CAST(struct mbridge *, mbridge_);
95     if (mbridge) {
96         ovs_refcount_ref(&mbridge->ref_cnt);
97     }
98     return mbridge;
99 }
100
101 void
102 mbridge_unref(struct mbridge *mbridge)
103 {
104     struct mbundle *mbundle, *next;
105     size_t i;
106
107     if (!mbridge) {
108         return;
109     }
110
111     if (ovs_refcount_unref(&mbridge->ref_cnt) == 1) {
112         for (i = 0; i < MAX_MIRRORS; i++) {
113             if (mbridge->mirrors[i]) {
114                 mirror_destroy(mbridge, mbridge->mirrors[i]->aux);
115             }
116         }
117
118         HMAP_FOR_EACH_SAFE (mbundle, next, hmap_node, &mbridge->mbundles) {
119             mbridge_unregister_bundle(mbridge, mbundle->ofbundle);
120         }
121
122         hmap_destroy(&mbridge->mbundles);
123         free(mbridge);
124     }
125 }
126
127 bool
128 mbridge_has_mirrors(struct mbridge *mbridge)
129 {
130     return mbridge ? mbridge->has_mirrors : false;
131 }
132
133 /* Returns true if configurations changes in 'mbridge''s mirrors require
134  * revalidation, and resets the revalidation flag to false. */
135 bool
136 mbridge_need_revalidate(struct mbridge *mbridge)
137 {
138     bool need_revalidate = mbridge->need_revalidate;
139     mbridge->need_revalidate = false;
140     return need_revalidate;
141 }
142
143 void
144 mbridge_register_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
145 {
146     struct mbundle *mbundle;
147
148     mbundle = xzalloc(sizeof *mbundle);
149     mbundle->ofbundle = ofbundle;
150     hmap_insert(&mbridge->mbundles, &mbundle->hmap_node,
151                 hash_pointer(ofbundle, 0));
152 }
153
154 void
155 mbridge_unregister_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
156 {
157     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
158     size_t i;
159
160     if (!mbundle) {
161         return;
162     }
163
164     for (i = 0; i < MAX_MIRRORS; i++) {
165         struct mirror *m = mbridge->mirrors[i];
166         if (m) {
167             if (m->out == mbundle) {
168                 mirror_destroy(mbridge, m->aux);
169             } else if (hmapx_find_and_delete(&m->srcs, mbundle)
170                        || hmapx_find_and_delete(&m->dsts, mbundle)) {
171                 mbridge->need_revalidate = true;
172             }
173         }
174     }
175
176     hmap_remove(&mbridge->mbundles, &mbundle->hmap_node);
177     free(mbundle);
178 }
179
180 mirror_mask_t
181 mirror_bundle_out(struct mbridge *mbridge, struct ofbundle *ofbundle)
182 {
183     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
184     return mbundle ? mbundle->mirror_out : 0;
185 }
186
187 mirror_mask_t
188 mirror_bundle_src(struct mbridge *mbridge, struct ofbundle *ofbundle)
189 {
190     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
191     return mbundle ? mbundle->src_mirrors : 0;
192 }
193
194 mirror_mask_t
195 mirror_bundle_dst(struct mbridge *mbridge, struct ofbundle *ofbundle)
196 {
197     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
198     return mbundle ? mbundle->dst_mirrors : 0;
199 }
200
201 int
202 mirror_set(struct mbridge *mbridge, void *aux, const char *name,
203            struct ofbundle **srcs, size_t n_srcs,
204            struct ofbundle **dsts, size_t n_dsts,
205            unsigned long *src_vlans, struct ofbundle *out_bundle,
206            uint16_t out_vlan)
207 {
208     struct mbundle *mbundle, *out;
209     mirror_mask_t mirror_bit;
210     struct mirror *mirror;
211     struct hmapx srcs_map;          /* Contains "struct ofbundle *"s. */
212     struct hmapx dsts_map;          /* Contains "struct ofbundle *"s. */
213
214     mirror = mirror_lookup(mbridge, aux);
215     if (!mirror) {
216         int idx;
217
218         idx = mirror_scan(mbridge);
219         if (idx < 0) {
220             VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
221                       MAX_MIRRORS, name);
222             return EFBIG;
223         }
224
225         mirror = mbridge->mirrors[idx] = xzalloc(sizeof *mirror);
226         mirror->mbridge = mbridge;
227         mirror->idx = idx;
228         mirror->aux = aux;
229         mirror->out_vlan = -1;
230     }
231
232     /* Get the new configuration. */
233     if (out_bundle) {
234         out = mbundle_lookup(mbridge, out_bundle);
235         if (!out) {
236             mirror_destroy(mbridge, mirror->aux);
237             return EINVAL;
238         }
239         out_vlan = -1;
240     } else {
241         out = NULL;
242     }
243     mbundle_lookup_multiple(mbridge, srcs, n_srcs, &srcs_map);
244     mbundle_lookup_multiple(mbridge, dsts, n_dsts, &dsts_map);
245
246     /* If the configuration has not changed, do nothing. */
247     if (hmapx_equals(&srcs_map, &mirror->srcs)
248         && hmapx_equals(&dsts_map, &mirror->dsts)
249         && vlan_bitmap_equal(mirror->vlans, src_vlans)
250         && mirror->out == out
251         && mirror->out_vlan == out_vlan)
252     {
253         hmapx_destroy(&srcs_map);
254         hmapx_destroy(&dsts_map);
255         return 0;
256     }
257
258     hmapx_swap(&srcs_map, &mirror->srcs);
259     hmapx_destroy(&srcs_map);
260
261     hmapx_swap(&dsts_map, &mirror->dsts);
262     hmapx_destroy(&dsts_map);
263
264     free(mirror->vlans);
265     mirror->vlans = vlan_bitmap_clone(src_vlans);
266
267     mirror->out = out;
268     mirror->out_vlan = out_vlan;
269
270     /* Update mbundles. */
271     mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
272     HMAP_FOR_EACH (mbundle, hmap_node, &mirror->mbridge->mbundles) {
273         if (hmapx_contains(&mirror->srcs, mbundle)) {
274             mbundle->src_mirrors |= mirror_bit;
275         } else {
276             mbundle->src_mirrors &= ~mirror_bit;
277         }
278
279         if (hmapx_contains(&mirror->dsts, mbundle)) {
280             mbundle->dst_mirrors |= mirror_bit;
281         } else {
282             mbundle->dst_mirrors &= ~mirror_bit;
283         }
284
285         if (mirror->out == mbundle) {
286             mbundle->mirror_out |= mirror_bit;
287         } else {
288             mbundle->mirror_out &= ~mirror_bit;
289         }
290     }
291
292     mbridge->has_mirrors = true;
293     mirror_update_dups(mbridge);
294
295     return 0;
296 }
297
298 void
299 mirror_destroy(struct mbridge *mbridge, void *aux)
300 {
301     struct mirror *mirror = mirror_lookup(mbridge, aux);
302     mirror_mask_t mirror_bit;
303     struct mbundle *mbundle;
304     int i;
305
306     if (!mirror) {
307         return;
308     }
309
310     mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
311     HMAP_FOR_EACH (mbundle, hmap_node, &mbridge->mbundles) {
312         mbundle->src_mirrors &= ~mirror_bit;
313         mbundle->dst_mirrors &= ~mirror_bit;
314         mbundle->mirror_out &= ~mirror_bit;
315     }
316
317     hmapx_destroy(&mirror->srcs);
318     hmapx_destroy(&mirror->dsts);
319     free(mirror->vlans);
320
321     mbridge->mirrors[mirror->idx] = NULL;
322     free(mirror);
323
324     mirror_update_dups(mbridge);
325
326     mbridge->has_mirrors = false;
327     for (i = 0; i < MAX_MIRRORS; i++) {
328         if (mbridge->mirrors[i]) {
329             mbridge->has_mirrors = true;
330             break;
331         }
332     }
333 }
334
335 int
336 mirror_get_stats(struct mbridge *mbridge, void *aux, uint64_t *packets,
337                  uint64_t *bytes)
338 {
339     struct mirror *mirror = mirror_lookup(mbridge, aux);
340
341     if (!mirror) {
342         *packets = *bytes = UINT64_MAX;
343         return 0;
344     }
345
346     *packets = mirror->packet_count;
347     *bytes = mirror->byte_count;
348
349     return 0;
350 }
351
352 void
353 mirror_update_stats(struct mbridge *mbridge, mirror_mask_t mirrors,
354                     uint64_t packets, uint64_t bytes)
355 {
356     if (!mbridge || !mirrors) {
357         return;
358     }
359
360     for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
361         struct mirror *m;
362
363         m = mbridge->mirrors[raw_ctz(mirrors)];
364
365         if (!m) {
366             /* In normal circumstances 'm' will not be NULL.  However,
367              * if mirrors are reconfigured, we can temporarily get out
368              * of sync in facet_revalidate().  We could "correct" the
369              * mirror list before reaching here, but doing that would
370              * not properly account the traffic stats we've currently
371              * accumulated for previous mirror configuration. */
372             continue;
373         }
374
375         m->packet_count += packets;
376         m->byte_count += bytes;
377     }
378 }
379
380 /* Retrieves the mirror numbered 'index' in 'mbridge'.  Returns true if such a
381  * mirror exists, false otherwise.
382  *
383  * If successful, '*vlans' receives the mirror's VLAN membership information,
384  * either a null pointer if the mirror includes all VLANs or a 4096-bit bitmap
385  * in which a 1-bit indicates that the mirror includes a particular VLAN,
386  * '*dup_mirrors' receives a bitmap of mirrors whose output duplicates mirror
387  * 'index', '*out' receives the output ofbundle (if any), and '*out_vlan'
388  * receives the output VLAN (if any). */
389 bool
390 mirror_get(struct mbridge *mbridge, int index, const unsigned long **vlans,
391            mirror_mask_t *dup_mirrors, struct ofbundle **out, int *out_vlan)
392 {
393     struct mirror *mirror;
394
395     if (!mbridge) {
396         return false;
397     }
398
399     mirror = mbridge->mirrors[index];
400     if (!mirror) {
401         return false;
402     }
403
404     *vlans = mirror->vlans;
405     *dup_mirrors = mirror->dup_mirrors;
406     *out = mirror->out ? mirror->out->ofbundle : NULL;
407     *out_vlan = mirror->out_vlan;
408     return true;
409 }
410 \f
411 /* Helpers. */
412
413 static struct mbundle *
414 mbundle_lookup(const struct mbridge *mbridge, struct ofbundle *ofbundle)
415 {
416     struct mbundle *mbundle;
417
418     HMAP_FOR_EACH_IN_BUCKET (mbundle, hmap_node, hash_pointer(ofbundle, 0),
419                              &mbridge->mbundles) {
420         if (mbundle->ofbundle == ofbundle) {
421             return mbundle;
422         }
423     }
424     return NULL;
425 }
426
427 /* Looks up each of the 'n_ofbundlees' pointers in 'ofbundlees' as mbundles and
428  * adds the ones that are found to 'mbundles'. */
429 static void
430 mbundle_lookup_multiple(const struct mbridge *mbridge,
431                         struct ofbundle **ofbundles, size_t n_ofbundles,
432                         struct hmapx *mbundles)
433 {
434     size_t i;
435
436     hmapx_init(mbundles);
437     for (i = 0; i < n_ofbundles; i++) {
438         struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundles[i]);
439         if (mbundle) {
440             hmapx_add(mbundles, mbundle);
441         }
442     }
443 }
444
445 static int
446 mirror_scan(struct mbridge *mbridge)
447 {
448     int idx;
449
450     for (idx = 0; idx < MAX_MIRRORS; idx++) {
451         if (!mbridge->mirrors[idx]) {
452             return idx;
453         }
454     }
455     return -1;
456 }
457
458 static struct mirror *
459 mirror_lookup(struct mbridge *mbridge, void *aux)
460 {
461     int i;
462
463     for (i = 0; i < MAX_MIRRORS; i++) {
464         struct mirror *mirror = mbridge->mirrors[i];
465         if (mirror && mirror->aux == aux) {
466             return mirror;
467         }
468     }
469
470     return NULL;
471 }
472
473 /* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
474 static void
475 mirror_update_dups(struct mbridge *mbridge)
476 {
477     int i;
478
479     for (i = 0; i < MAX_MIRRORS; i++) {
480         struct mirror *m = mbridge->mirrors[i];
481
482         if (m) {
483             m->dup_mirrors = MIRROR_MASK_C(1) << i;
484         }
485     }
486
487     for (i = 0; i < MAX_MIRRORS; i++) {
488         struct mirror *m1 = mbridge->mirrors[i];
489         int j;
490
491         if (!m1) {
492             continue;
493         }
494
495         for (j = i + 1; j < MAX_MIRRORS; j++) {
496             struct mirror *m2 = mbridge->mirrors[j];
497
498             if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
499                 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
500                 m2->dup_mirrors |= m1->dup_mirrors;
501             }
502         }
503     }
504 }