Avoid printf type modifiers not supported by MSVC C runtime library.
[cascardo/ovs.git] / ofproto / ofproto-dpif-mirror.c
1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 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 "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     int 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 static int mirror_mask_ffs(mirror_mask_t);
79
80 struct mbridge *
81 mbridge_create(void)
82 {
83     struct mbridge *mbridge;
84
85     mbridge = xzalloc(sizeof *mbridge);
86     mbridge->ref_cnt = 1;
87
88     hmap_init(&mbridge->mbundles);
89     return mbridge;
90 }
91
92 struct mbridge *
93 mbridge_ref(const struct mbridge *mbridge_)
94 {
95     struct mbridge *mbridge = CONST_CAST(struct mbridge *, mbridge_);
96     if (mbridge) {
97         ovs_assert(mbridge->ref_cnt > 0);
98         mbridge->ref_cnt++;
99     }
100     return mbridge;
101 }
102
103 void
104 mbridge_unref(struct mbridge *mbridge)
105 {
106     struct mbundle *mbundle, *next;
107     size_t i;
108
109     if (!mbridge) {
110         return;
111     }
112
113     ovs_assert(mbridge->ref_cnt > 0);
114     if (--mbridge->ref_cnt) {
115         return;
116     }
117
118     for (i = 0; i < MAX_MIRRORS; i++) {
119         if (mbridge->mirrors[i]) {
120             mirror_destroy(mbridge, mbridge->mirrors[i]->aux);
121         }
122     }
123
124     HMAP_FOR_EACH_SAFE (mbundle, next, hmap_node, &mbridge->mbundles) {
125         mbridge_unregister_bundle(mbridge, mbundle->ofbundle);
126     }
127
128     hmap_destroy(&mbridge->mbundles);
129     free(mbridge);
130 }
131
132 bool
133 mbridge_has_mirrors(struct mbridge *mbridge)
134 {
135     return mbridge ? mbridge->has_mirrors : false;
136 }
137
138 /* Returns true if configurations changes in 'mbridge''s mirrors require
139  * revalidation. */
140 bool
141 mbridge_need_revalidate(struct mbridge *mbridge)
142 {
143     return mbridge->need_revalidate;
144 }
145
146 void
147 mbridge_register_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
148 {
149     struct mbundle *mbundle;
150
151     mbundle = xzalloc(sizeof *mbundle);
152     mbundle->ofbundle = ofbundle;
153     hmap_insert(&mbridge->mbundles, &mbundle->hmap_node,
154                 hash_pointer(ofbundle, 0));
155 }
156
157 void
158 mbridge_unregister_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
159 {
160     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
161     size_t i;
162
163     if (!mbundle) {
164         return;
165     }
166
167     for (i = 0; i < MAX_MIRRORS; i++) {
168         struct mirror *m = mbridge->mirrors[i];
169         if (m) {
170             if (m->out == mbundle) {
171                 mirror_destroy(mbridge, m->aux);
172             } else if (hmapx_find_and_delete(&m->srcs, mbundle)
173                        || hmapx_find_and_delete(&m->dsts, mbundle)) {
174                 mbridge->need_revalidate = true;
175             }
176         }
177     }
178
179     hmap_remove(&mbridge->mbundles, &mbundle->hmap_node);
180     free(mbundle);
181 }
182
183 mirror_mask_t
184 mirror_bundle_out(struct mbridge *mbridge, struct ofbundle *ofbundle)
185 {
186     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
187     return mbundle ? mbundle->mirror_out : 0;
188 }
189
190 mirror_mask_t
191 mirror_bundle_src(struct mbridge *mbridge, struct ofbundle *ofbundle)
192 {
193     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
194     return mbundle ? mbundle->src_mirrors : 0;
195 }
196
197 mirror_mask_t
198 mirror_bundle_dst(struct mbridge *mbridge, struct ofbundle *ofbundle)
199 {
200     struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
201     return mbundle ? mbundle->dst_mirrors : 0;
202 }
203
204 int
205 mirror_set(struct mbridge *mbridge, void *aux, const char *name,
206            struct ofbundle **srcs, size_t n_srcs,
207            struct ofbundle **dsts, size_t n_dsts,
208            unsigned long *src_vlans, struct ofbundle *out_bundle,
209            uint16_t out_vlan)
210 {
211     struct mbundle *mbundle, *out;
212     mirror_mask_t mirror_bit;
213     struct mirror *mirror;
214     struct hmapx srcs_map;          /* Contains "struct ofbundle *"s. */
215     struct hmapx dsts_map;          /* Contains "struct ofbundle *"s. */
216
217     mirror = mirror_lookup(mbridge, aux);
218     if (!mirror) {
219         int idx;
220
221         idx = mirror_scan(mbridge);
222         if (idx < 0) {
223             VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
224                       MAX_MIRRORS, name);
225             return EFBIG;
226         }
227
228         mirror = mbridge->mirrors[idx] = xzalloc(sizeof *mirror);
229         mirror->mbridge = mbridge;
230         mirror->idx = idx;
231         mirror->aux = aux;
232         mirror->out_vlan = -1;
233     }
234
235     /* Get the new configuration. */
236     if (out_bundle) {
237         out = mbundle_lookup(mbridge, out_bundle);
238         if (!out) {
239             mirror_destroy(mbridge, mirror->aux);
240             return EINVAL;
241         }
242         out_vlan = -1;
243     } else {
244         out = NULL;
245     }
246     mbundle_lookup_multiple(mbridge, srcs, n_srcs, &srcs_map);
247     mbundle_lookup_multiple(mbridge, dsts, n_dsts, &dsts_map);
248
249     /* If the configuration has not changed, do nothing. */
250     if (hmapx_equals(&srcs_map, &mirror->srcs)
251         && hmapx_equals(&dsts_map, &mirror->dsts)
252         && vlan_bitmap_equal(mirror->vlans, src_vlans)
253         && mirror->out == out
254         && mirror->out_vlan == out_vlan)
255     {
256         hmapx_destroy(&srcs_map);
257         hmapx_destroy(&dsts_map);
258         return 0;
259     }
260
261     hmapx_swap(&srcs_map, &mirror->srcs);
262     hmapx_destroy(&srcs_map);
263
264     hmapx_swap(&dsts_map, &mirror->dsts);
265     hmapx_destroy(&dsts_map);
266
267     free(mirror->vlans);
268     mirror->vlans = vlan_bitmap_clone(src_vlans);
269
270     mirror->out = out;
271     mirror->out_vlan = out_vlan;
272
273     /* Update mbundles. */
274     mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
275     HMAP_FOR_EACH (mbundle, hmap_node, &mirror->mbridge->mbundles) {
276         if (hmapx_contains(&mirror->srcs, mbundle)) {
277             mbundle->src_mirrors |= mirror_bit;
278         } else {
279             mbundle->src_mirrors &= ~mirror_bit;
280         }
281
282         if (hmapx_contains(&mirror->dsts, mbundle)) {
283             mbundle->dst_mirrors |= mirror_bit;
284         } else {
285             mbundle->dst_mirrors &= ~mirror_bit;
286         }
287
288         if (mirror->out == mbundle) {
289             mbundle->mirror_out |= mirror_bit;
290         } else {
291             mbundle->mirror_out &= ~mirror_bit;
292         }
293     }
294
295     mbridge->has_mirrors = true;
296     mirror_update_dups(mbridge);
297
298     return 0;
299 }
300
301 void
302 mirror_destroy(struct mbridge *mbridge, void *aux)
303 {
304     struct mirror *mirror = mirror_lookup(mbridge, aux);
305     mirror_mask_t mirror_bit;
306     struct mbundle *mbundle;
307     int i;
308
309     if (!mirror) {
310         return;
311     }
312
313     mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
314     HMAP_FOR_EACH (mbundle, hmap_node, &mbridge->mbundles) {
315         mbundle->src_mirrors &= ~mirror_bit;
316         mbundle->dst_mirrors &= ~mirror_bit;
317         mbundle->mirror_out &= ~mirror_bit;
318     }
319
320     hmapx_destroy(&mirror->srcs);
321     hmapx_destroy(&mirror->dsts);
322     free(mirror->vlans);
323
324     mbridge->mirrors[mirror->idx] = NULL;
325     free(mirror);
326
327     mirror_update_dups(mbridge);
328
329     mbridge->has_mirrors = false;
330     for (i = 0; i < MAX_MIRRORS; i++) {
331         if (mbridge->mirrors[i]) {
332             mbridge->has_mirrors = true;
333             break;
334         }
335     }
336 }
337
338 int
339 mirror_get_stats(struct mbridge *mbridge, void *aux, uint64_t *packets,
340                  uint64_t *bytes)
341 {
342     struct mirror *mirror = mirror_lookup(mbridge, aux);
343
344     if (!mirror) {
345         *packets = *bytes = UINT64_MAX;
346         return 0;
347     }
348
349     *packets = mirror->packet_count;
350     *bytes = mirror->byte_count;
351
352     return 0;
353 }
354
355 void
356 mirror_update_stats(struct mbridge *mbridge, mirror_mask_t mirrors,
357                     uint64_t packets, uint64_t bytes)
358 {
359     if (!mbridge || !mirrors) {
360         return;
361     }
362
363     for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
364         struct mirror *m;
365
366         m = mbridge->mirrors[mirror_mask_ffs(mirrors) - 1];
367
368         if (!m) {
369             /* In normal circumstances 'm' will not be NULL.  However,
370              * if mirrors are reconfigured, we can temporarily get out
371              * of sync in facet_revalidate().  We could "correct" the
372              * mirror list before reaching here, but doing that would
373              * not properly account the traffic stats we've currently
374              * accumulated for previous mirror configuration. */
375             continue;
376         }
377
378         m->packet_count += packets;
379         m->byte_count += bytes;
380     }
381 }
382
383 /* Retrieves the mirror in 'mbridge' represented by the first bet set of
384  * 'mirrors'.  Returns true if such a mirror exists, false otherwise.
385  * The caller takes ownership of, and is expected to deallocate, 'vlans' */
386 bool
387 mirror_get(struct mbridge *mbridge, int index, unsigned long **vlans,
388            mirror_mask_t *dup_mirrors, struct ofbundle **out, int *out_vlan)
389 {
390     struct mirror *mirror;
391
392     if (!mbridge) {
393         return false;
394     }
395
396     mirror = mbridge->mirrors[index];
397     if (!mirror) {
398         return false;
399     }
400
401     *vlans = vlan_bitmap_clone(mirror->vlans);
402     *dup_mirrors = mirror->dup_mirrors;
403     *out = mirror->out ? mirror->out->ofbundle : NULL;
404     *out_vlan = mirror->out_vlan;
405     return true;
406 }
407 \f
408 /* Helpers. */
409
410 static struct mbundle *
411 mbundle_lookup(const struct mbridge *mbridge, struct ofbundle *ofbundle)
412 {
413     struct mbundle *mbundle;
414
415     HMAP_FOR_EACH_IN_BUCKET (mbundle, hmap_node, hash_pointer(ofbundle, 0),
416                              &mbridge->mbundles) {
417         if (mbundle->ofbundle == ofbundle) {
418             return mbundle;
419         }
420     }
421     return NULL;
422 }
423
424 /* Looks up each of the 'n_ofbundlees' pointers in 'ofbundlees' as mbundles and
425  * adds the ones that are found to 'mbundles'. */
426 static void
427 mbundle_lookup_multiple(const struct mbridge *mbridge,
428                         struct ofbundle **ofbundles, size_t n_ofbundles,
429                         struct hmapx *mbundles)
430 {
431     size_t i;
432
433     hmapx_init(mbundles);
434     for (i = 0; i < n_ofbundles; i++) {
435         struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundles[i]);
436         if (mbundle) {
437             hmapx_add(mbundles, mbundle);
438         }
439     }
440 }
441
442 static int
443 mirror_scan(struct mbridge *mbridge)
444 {
445     int idx;
446
447     for (idx = 0; idx < MAX_MIRRORS; idx++) {
448         if (!mbridge->mirrors[idx]) {
449             return idx;
450         }
451     }
452     return -1;
453 }
454
455 static struct mirror *
456 mirror_lookup(struct mbridge *mbridge, void *aux)
457 {
458     int i;
459
460     for (i = 0; i < MAX_MIRRORS; i++) {
461         struct mirror *mirror = mbridge->mirrors[i];
462         if (mirror && mirror->aux == aux) {
463             return mirror;
464         }
465     }
466
467     return NULL;
468 }
469
470 /* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
471 static void
472 mirror_update_dups(struct mbridge *mbridge)
473 {
474     int i;
475
476     for (i = 0; i < MAX_MIRRORS; i++) {
477         struct mirror *m = mbridge->mirrors[i];
478
479         if (m) {
480             m->dup_mirrors = MIRROR_MASK_C(1) << i;
481         }
482     }
483
484     for (i = 0; i < MAX_MIRRORS; i++) {
485         struct mirror *m1 = mbridge->mirrors[i];
486         int j;
487
488         if (!m1) {
489             continue;
490         }
491
492         for (j = i + 1; j < MAX_MIRRORS; j++) {
493             struct mirror *m2 = mbridge->mirrors[j];
494
495             if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
496                 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
497                 m2->dup_mirrors |= m1->dup_mirrors;
498             }
499         }
500     }
501 }