1 /* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2015 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"
25 #include "openvswitch/vlog.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
];
39 struct ovs_refcount ref_cnt
;
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
*);
82 struct mbridge
*mbridge
;
84 mbridge
= xzalloc(sizeof *mbridge
);
85 ovs_refcount_init(&mbridge
->ref_cnt
);
87 hmap_init(&mbridge
->mbundles
);
92 mbridge_ref(const struct mbridge
*mbridge_
)
94 struct mbridge
*mbridge
= CONST_CAST(struct mbridge
*, mbridge_
);
96 ovs_refcount_ref(&mbridge
->ref_cnt
);
102 mbridge_unref(struct mbridge
*mbridge
)
104 struct mbundle
*mbundle
, *next
;
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
);
118 HMAP_FOR_EACH_SAFE (mbundle
, next
, hmap_node
, &mbridge
->mbundles
) {
119 mbridge_unregister_bundle(mbridge
, mbundle
->ofbundle
);
122 hmap_destroy(&mbridge
->mbundles
);
128 mbridge_has_mirrors(struct mbridge
*mbridge
)
130 return mbridge
? mbridge
->has_mirrors
: false;
133 /* Returns true if configurations changes in 'mbridge''s mirrors require
136 mbridge_need_revalidate(struct mbridge
*mbridge
)
138 return mbridge
->need_revalidate
;
142 mbridge_register_bundle(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
144 struct mbundle
*mbundle
;
146 mbundle
= xzalloc(sizeof *mbundle
);
147 mbundle
->ofbundle
= ofbundle
;
148 hmap_insert(&mbridge
->mbundles
, &mbundle
->hmap_node
,
149 hash_pointer(ofbundle
, 0));
153 mbridge_unregister_bundle(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
155 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
162 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
163 struct mirror
*m
= mbridge
->mirrors
[i
];
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;
174 hmap_remove(&mbridge
->mbundles
, &mbundle
->hmap_node
);
179 mirror_bundle_out(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
181 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
182 return mbundle
? mbundle
->mirror_out
: 0;
186 mirror_bundle_src(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
188 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
189 return mbundle
? mbundle
->src_mirrors
: 0;
193 mirror_bundle_dst(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
195 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
196 return mbundle
? mbundle
->dst_mirrors
: 0;
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
,
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. */
212 mirror
= mirror_lookup(mbridge
, aux
);
216 idx
= mirror_scan(mbridge
);
218 VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
223 mirror
= mbridge
->mirrors
[idx
] = xzalloc(sizeof *mirror
);
224 mirror
->mbridge
= mbridge
;
227 mirror
->out_vlan
= -1;
230 /* Get the new configuration. */
232 out
= mbundle_lookup(mbridge
, out_bundle
);
234 mirror_destroy(mbridge
, mirror
->aux
);
241 mbundle_lookup_multiple(mbridge
, srcs
, n_srcs
, &srcs_map
);
242 mbundle_lookup_multiple(mbridge
, dsts
, n_dsts
, &dsts_map
);
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
)
251 hmapx_destroy(&srcs_map
);
252 hmapx_destroy(&dsts_map
);
256 hmapx_swap(&srcs_map
, &mirror
->srcs
);
257 hmapx_destroy(&srcs_map
);
259 hmapx_swap(&dsts_map
, &mirror
->dsts
);
260 hmapx_destroy(&dsts_map
);
263 mirror
->vlans
= vlan_bitmap_clone(src_vlans
);
266 mirror
->out_vlan
= out_vlan
;
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
;
274 mbundle
->src_mirrors
&= ~mirror_bit
;
277 if (hmapx_contains(&mirror
->dsts
, mbundle
)) {
278 mbundle
->dst_mirrors
|= mirror_bit
;
280 mbundle
->dst_mirrors
&= ~mirror_bit
;
283 if (mirror
->out
== mbundle
) {
284 mbundle
->mirror_out
|= mirror_bit
;
286 mbundle
->mirror_out
&= ~mirror_bit
;
290 mbridge
->has_mirrors
= true;
291 mirror_update_dups(mbridge
);
297 mirror_destroy(struct mbridge
*mbridge
, void *aux
)
299 struct mirror
*mirror
= mirror_lookup(mbridge
, aux
);
300 mirror_mask_t mirror_bit
;
301 struct mbundle
*mbundle
;
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
;
315 hmapx_destroy(&mirror
->srcs
);
316 hmapx_destroy(&mirror
->dsts
);
319 mbridge
->mirrors
[mirror
->idx
] = NULL
;
322 mirror_update_dups(mbridge
);
324 mbridge
->has_mirrors
= false;
325 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
326 if (mbridge
->mirrors
[i
]) {
327 mbridge
->has_mirrors
= true;
334 mirror_get_stats(struct mbridge
*mbridge
, void *aux
, uint64_t *packets
,
337 struct mirror
*mirror
= mirror_lookup(mbridge
, aux
);
340 *packets
= *bytes
= UINT64_MAX
;
344 *packets
= mirror
->packet_count
;
345 *bytes
= mirror
->byte_count
;
351 mirror_update_stats(struct mbridge
*mbridge
, mirror_mask_t mirrors
,
352 uint64_t packets
, uint64_t bytes
)
354 if (!mbridge
|| !mirrors
) {
358 for (; mirrors
; mirrors
= zero_rightmost_1bit(mirrors
)) {
361 m
= mbridge
->mirrors
[raw_ctz(mirrors
)];
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. */
373 m
->packet_count
+= packets
;
374 m
->byte_count
+= bytes
;
378 /* Retrieves the mirror numbered 'index' in 'mbridge'. Returns true if such a
379 * mirror exists, false otherwise.
381 * If successful, '*vlans' receives the mirror's VLAN membership information,
382 * either a null pointer if the mirror includes all VLANs or a 4096-bit bitmap
383 * in which a 1-bit indicates that the mirror includes a particular VLAN,
384 * '*dup_mirrors' receives a bitmap of mirrors whose output duplicates mirror
385 * 'index', '*out' receives the output ofbundle (if any), and '*out_vlan'
386 * receives the output VLAN (if any). */
388 mirror_get(struct mbridge
*mbridge
, int index
, const unsigned long **vlans
,
389 mirror_mask_t
*dup_mirrors
, struct ofbundle
**out
, int *out_vlan
)
391 struct mirror
*mirror
;
397 mirror
= mbridge
->mirrors
[index
];
402 *vlans
= mirror
->vlans
;
403 *dup_mirrors
= mirror
->dup_mirrors
;
404 *out
= mirror
->out
? mirror
->out
->ofbundle
: NULL
;
405 *out_vlan
= mirror
->out_vlan
;
411 static struct mbundle
*
412 mbundle_lookup(const struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
414 struct mbundle
*mbundle
;
416 HMAP_FOR_EACH_IN_BUCKET (mbundle
, hmap_node
, hash_pointer(ofbundle
, 0),
417 &mbridge
->mbundles
) {
418 if (mbundle
->ofbundle
== ofbundle
) {
425 /* Looks up each of the 'n_ofbundlees' pointers in 'ofbundlees' as mbundles and
426 * adds the ones that are found to 'mbundles'. */
428 mbundle_lookup_multiple(const struct mbridge
*mbridge
,
429 struct ofbundle
**ofbundles
, size_t n_ofbundles
,
430 struct hmapx
*mbundles
)
434 hmapx_init(mbundles
);
435 for (i
= 0; i
< n_ofbundles
; i
++) {
436 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundles
[i
]);
438 hmapx_add(mbundles
, mbundle
);
444 mirror_scan(struct mbridge
*mbridge
)
448 for (idx
= 0; idx
< MAX_MIRRORS
; idx
++) {
449 if (!mbridge
->mirrors
[idx
]) {
456 static struct mirror
*
457 mirror_lookup(struct mbridge
*mbridge
, void *aux
)
461 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
462 struct mirror
*mirror
= mbridge
->mirrors
[i
];
463 if (mirror
&& mirror
->aux
== aux
) {
471 /* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
473 mirror_update_dups(struct mbridge
*mbridge
)
477 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
478 struct mirror
*m
= mbridge
->mirrors
[i
];
481 m
->dup_mirrors
= MIRROR_MASK_C(1) << i
;
485 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
486 struct mirror
*m1
= mbridge
->mirrors
[i
];
493 for (j
= i
+ 1; j
< MAX_MIRRORS
; j
++) {
494 struct mirror
*m2
= mbridge
->mirrors
[j
];
496 if (m2
&& m1
->out
== m2
->out
&& m1
->out_vlan
== m2
->out_vlan
) {
497 m1
->dup_mirrors
|= MIRROR_MASK_C(1) << j
;
498 m2
->dup_mirrors
|= m1
->dup_mirrors
;