1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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. */
17 #include "ofproto-dpif-mirror.h"
24 #include "vlan-bitmap.h"
27 VLOG_DEFINE_THIS_MODULE(ofproto_dpif_mirror
);
29 #define MIRROR_MASK_C(X) UINT32_C(X)
30 BUILD_ASSERT_DECL(sizeof(mirror_mask_t
) * CHAR_BIT
>= MAX_MIRRORS
);
33 struct mirror
*mirrors
[MAX_MIRRORS
];
43 struct hmap_node hmap_node
; /* In parent 'mbridge' map. */
44 struct ofbundle
*ofbundle
;
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. */
52 struct mbridge
*mbridge
; /* Owning ofproto. */
53 size_t idx
; /* In ofproto's "mirrors" array. */
54 void *aux
; /* Key supplied by ofproto's client. */
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. */
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. */
67 int64_t packet_count
; /* Number of packets sent. */
68 int64_t byte_count
; /* Number of bytes sent. */
71 static struct mirror
*mirror_lookup(struct mbridge
*, void *aux
);
72 static struct mbundle
*mbundle_lookup(const struct mbridge
*,
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
);
83 struct mbridge
*mbridge
;
85 mbridge
= xzalloc(sizeof *mbridge
);
88 hmap_init(&mbridge
->mbundles
);
93 mbridge_ref(const struct mbridge
*mbridge_
)
95 struct mbridge
*mbridge
= CONST_CAST(struct mbridge
*, mbridge_
);
97 ovs_assert(mbridge
->ref_cnt
> 0);
104 mbridge_unref(struct mbridge
*mbridge
)
106 struct mbundle
*mbundle
, *next
;
113 ovs_assert(mbridge
->ref_cnt
> 0);
114 if (--mbridge
->ref_cnt
) {
118 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
119 if (mbridge
->mirrors
[i
]) {
120 mirror_destroy(mbridge
, mbridge
->mirrors
[i
]->aux
);
124 HMAP_FOR_EACH_SAFE (mbundle
, next
, hmap_node
, &mbridge
->mbundles
) {
125 mbridge_unregister_bundle(mbridge
, mbundle
->ofbundle
);
128 hmap_destroy(&mbridge
->mbundles
);
133 mbridge_has_mirrors(struct mbridge
*mbridge
)
135 return mbridge
? mbridge
->has_mirrors
: false;
138 /* Returns true if configurations changes in 'mbridge''s mirrors require
141 mbridge_need_revalidate(struct mbridge
*mbridge
)
143 return mbridge
->need_revalidate
;
147 mbridge_register_bundle(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
149 struct mbundle
*mbundle
;
151 mbundle
= xzalloc(sizeof *mbundle
);
152 mbundle
->ofbundle
= ofbundle
;
153 hmap_insert(&mbridge
->mbundles
, &mbundle
->hmap_node
,
154 hash_pointer(ofbundle
, 0));
158 mbridge_unregister_bundle(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
160 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
167 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
168 struct mirror
*m
= mbridge
->mirrors
[i
];
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;
179 hmap_remove(&mbridge
->mbundles
, &mbundle
->hmap_node
);
184 mirror_bundle_out(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
186 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
187 return mbundle
? mbundle
->mirror_out
: 0;
191 mirror_bundle_src(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
193 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
194 return mbundle
? mbundle
->src_mirrors
: 0;
198 mirror_bundle_dst(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
200 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
201 return mbundle
? mbundle
->dst_mirrors
: 0;
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
,
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. */
217 mirror
= mirror_lookup(mbridge
, aux
);
221 idx
= mirror_scan(mbridge
);
223 VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
228 mirror
= mbridge
->mirrors
[idx
] = xzalloc(sizeof *mirror
);
229 mirror
->mbridge
= mbridge
;
232 mirror
->out_vlan
= -1;
235 /* Get the new configuration. */
237 out
= mbundle_lookup(mbridge
, out_bundle
);
239 mirror_destroy(mbridge
, mirror
->aux
);
246 mbundle_lookup_multiple(mbridge
, srcs
, n_srcs
, &srcs_map
);
247 mbundle_lookup_multiple(mbridge
, dsts
, n_dsts
, &dsts_map
);
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
)
256 hmapx_destroy(&srcs_map
);
257 hmapx_destroy(&dsts_map
);
261 hmapx_swap(&srcs_map
, &mirror
->srcs
);
262 hmapx_destroy(&srcs_map
);
264 hmapx_swap(&dsts_map
, &mirror
->dsts
);
265 hmapx_destroy(&dsts_map
);
268 mirror
->vlans
= vlan_bitmap_clone(src_vlans
);
271 mirror
->out_vlan
= out_vlan
;
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
;
279 mbundle
->src_mirrors
&= ~mirror_bit
;
282 if (hmapx_contains(&mirror
->dsts
, mbundle
)) {
283 mbundle
->dst_mirrors
|= mirror_bit
;
285 mbundle
->dst_mirrors
&= ~mirror_bit
;
288 if (mirror
->out
== mbundle
) {
289 mbundle
->mirror_out
|= mirror_bit
;
291 mbundle
->mirror_out
&= ~mirror_bit
;
295 mbridge
->has_mirrors
= true;
296 mirror_update_dups(mbridge
);
302 mirror_destroy(struct mbridge
*mbridge
, void *aux
)
304 struct mirror
*mirror
= mirror_lookup(mbridge
, aux
);
305 mirror_mask_t mirror_bit
;
306 struct mbundle
*mbundle
;
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
;
320 hmapx_destroy(&mirror
->srcs
);
321 hmapx_destroy(&mirror
->dsts
);
324 mbridge
->mirrors
[mirror
->idx
] = NULL
;
327 mirror_update_dups(mbridge
);
329 mbridge
->has_mirrors
= false;
330 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
331 if (mbridge
->mirrors
[i
]) {
332 mbridge
->has_mirrors
= true;
339 mirror_get_stats(struct mbridge
*mbridge
, void *aux
, uint64_t *packets
,
342 struct mirror
*mirror
= mirror_lookup(mbridge
, aux
);
345 *packets
= *bytes
= UINT64_MAX
;
349 *packets
= mirror
->packet_count
;
350 *bytes
= mirror
->byte_count
;
356 mirror_update_stats(struct mbridge
*mbridge
, mirror_mask_t mirrors
,
357 uint64_t packets
, uint64_t bytes
)
359 if (!mbridge
|| !mirrors
) {
363 for (; mirrors
; mirrors
= zero_rightmost_1bit(mirrors
)) {
366 m
= mbridge
->mirrors
[mirror_mask_ffs(mirrors
) - 1];
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. */
378 m
->packet_count
+= packets
;
379 m
->byte_count
+= bytes
;
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' */
387 mirror_get(struct mbridge
*mbridge
, int index
, unsigned long **vlans
,
388 mirror_mask_t
*dup_mirrors
, struct ofbundle
**out
, int *out_vlan
)
390 struct mirror
*mirror
;
396 mirror
= mbridge
->mirrors
[index
];
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
;
410 static struct mbundle
*
411 mbundle_lookup(const struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
413 struct mbundle
*mbundle
;
415 HMAP_FOR_EACH_IN_BUCKET (mbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
416 &mbridge
->mbundles
) {
417 if (mbundle
->ofbundle
== ofbundle
) {
424 /* Looks up each of the 'n_ofbundlees' pointers in 'ofbundlees' as mbundles and
425 * adds the ones that are found to 'mbundles'. */
427 mbundle_lookup_multiple(const struct mbridge
*mbridge
,
428 struct ofbundle
**ofbundles
, size_t n_ofbundles
,
429 struct hmapx
*mbundles
)
433 hmapx_init(mbundles
);
434 for (i
= 0; i
< n_ofbundles
; i
++) {
435 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundles
[i
]);
437 hmapx_add(mbundles
, mbundle
);
443 mirror_scan(struct mbridge
*mbridge
)
447 for (idx
= 0; idx
< MAX_MIRRORS
; idx
++) {
448 if (!mbridge
->mirrors
[idx
]) {
455 static struct mirror
*
456 mirror_lookup(struct mbridge
*mbridge
, void *aux
)
460 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
461 struct mirror
*mirror
= mbridge
->mirrors
[i
];
462 if (mirror
&& mirror
->aux
== aux
) {
470 /* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
472 mirror_update_dups(struct mbridge
*mbridge
)
476 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
477 struct mirror
*m
= mbridge
->mirrors
[i
];
480 m
->dup_mirrors
= MIRROR_MASK_C(1) << i
;
484 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
485 struct mirror
*m1
= mbridge
->mirrors
[i
];
492 for (j
= i
+ 1; j
< MAX_MIRRORS
; j
++) {
493 struct mirror
*m2
= mbridge
->mirrors
[j
];
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
;