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