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 cmap_node cmap_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. */
60 /* This is accessed by handler threads assuming RCU protection (see
61 * mirror_get()), but can be manipulated by mirror_set() without any
62 * explicit synchronization. */
63 OVSRCU_TYPE(unsigned long *) vlans
; /* Bitmap of chosen VLANs, NULL
66 /* Output (exactly one of out == NULL and out_vlan == -1 is true). */
67 struct mbundle
*out
; /* Output port or NULL. */
68 int out_vlan
; /* Output VLAN or -1. */
69 uint16_t snaplen
; /* Max per mirrored packet size in byte,
70 set to 0 equals 65535. */
71 mirror_mask_t dup_mirrors
; /* Bitmap of mirrors with the same output. */
74 int64_t packet_count
; /* Number of packets sent. */
75 int64_t byte_count
; /* Number of bytes sent. */
78 static struct mirror
*mirror_lookup(struct mbridge
*, void *aux
);
79 static struct mbundle
*mbundle_lookup(const struct mbridge
*,
81 static void mbundle_lookup_multiple(const struct mbridge
*, struct ofbundle
**,
82 size_t n_bundles
, struct hmapx
*mbundles
);
83 static int mirror_scan(struct mbridge
*);
84 static void mirror_update_dups(struct mbridge
*);
89 struct mbridge
*mbridge
;
91 mbridge
= xzalloc(sizeof *mbridge
);
92 ovs_refcount_init(&mbridge
->ref_cnt
);
94 cmap_init(&mbridge
->mbundles
);
99 mbridge_ref(const struct mbridge
*mbridge_
)
101 struct mbridge
*mbridge
= CONST_CAST(struct mbridge
*, mbridge_
);
103 ovs_refcount_ref(&mbridge
->ref_cnt
);
109 mbridge_unref(struct mbridge
*mbridge
)
111 struct mbundle
*mbundle
;
118 if (ovs_refcount_unref(&mbridge
->ref_cnt
) == 1) {
119 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
120 if (mbridge
->mirrors
[i
]) {
121 mirror_destroy(mbridge
, mbridge
->mirrors
[i
]->aux
);
125 CMAP_FOR_EACH (mbundle
, cmap_node
, &mbridge
->mbundles
) {
126 mbridge_unregister_bundle(mbridge
, mbundle
->ofbundle
);
129 cmap_destroy(&mbridge
->mbundles
);
130 ovsrcu_postpone(free
, mbridge
);
135 mbridge_has_mirrors(struct mbridge
*mbridge
)
137 return mbridge
? mbridge
->has_mirrors
: false;
140 /* Returns true if configurations changes in 'mbridge''s mirrors require
141 * revalidation, and resets the revalidation flag to false. */
143 mbridge_need_revalidate(struct mbridge
*mbridge
)
145 bool need_revalidate
= mbridge
->need_revalidate
;
146 mbridge
->need_revalidate
= false;
147 return need_revalidate
;
151 mbridge_register_bundle(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
153 struct mbundle
*mbundle
;
155 mbundle
= xzalloc(sizeof *mbundle
);
156 mbundle
->ofbundle
= ofbundle
;
157 cmap_insert(&mbridge
->mbundles
, &mbundle
->cmap_node
,
158 hash_pointer(ofbundle
, 0));
162 mbridge_unregister_bundle(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
164 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
171 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
172 struct mirror
*m
= mbridge
->mirrors
[i
];
174 if (m
->out
== mbundle
) {
175 mirror_destroy(mbridge
, m
->aux
);
176 } else if (hmapx_find_and_delete(&m
->srcs
, mbundle
)
177 || hmapx_find_and_delete(&m
->dsts
, mbundle
)) {
178 mbridge
->need_revalidate
= true;
183 cmap_remove(&mbridge
->mbundles
, &mbundle
->cmap_node
,
184 hash_pointer(ofbundle
, 0));
185 ovsrcu_postpone(free
, mbundle
);
189 mirror_bundle_out(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
191 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
192 return mbundle
? mbundle
->mirror_out
: 0;
196 mirror_bundle_src(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
198 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
199 return mbundle
? mbundle
->src_mirrors
: 0;
203 mirror_bundle_dst(struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
205 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundle
);
206 return mbundle
? mbundle
->dst_mirrors
: 0;
210 mirror_set(struct mbridge
*mbridge
, void *aux
, const char *name
,
211 struct ofbundle
**srcs
, size_t n_srcs
,
212 struct ofbundle
**dsts
, size_t n_dsts
,
213 unsigned long *src_vlans
, struct ofbundle
*out_bundle
,
217 struct mbundle
*mbundle
, *out
;
218 mirror_mask_t mirror_bit
;
219 struct mirror
*mirror
;
220 struct hmapx srcs_map
; /* Contains "struct ofbundle *"s. */
221 struct hmapx dsts_map
; /* Contains "struct ofbundle *"s. */
223 mirror
= mirror_lookup(mbridge
, aux
);
227 idx
= mirror_scan(mbridge
);
229 VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
234 mirror
= mbridge
->mirrors
[idx
] = xzalloc(sizeof *mirror
);
235 mirror
->mbridge
= mbridge
;
238 mirror
->out_vlan
= -1;
242 unsigned long *vlans
= ovsrcu_get(unsigned long *, &mirror
->vlans
);
244 /* Get the new configuration. */
246 out
= mbundle_lookup(mbridge
, out_bundle
);
248 mirror_destroy(mbridge
, mirror
->aux
);
255 mbundle_lookup_multiple(mbridge
, srcs
, n_srcs
, &srcs_map
);
256 mbundle_lookup_multiple(mbridge
, dsts
, n_dsts
, &dsts_map
);
258 /* If the configuration has not changed, do nothing. */
259 if (hmapx_equals(&srcs_map
, &mirror
->srcs
)
260 && hmapx_equals(&dsts_map
, &mirror
->dsts
)
261 && vlan_bitmap_equal(vlans
, src_vlans
)
262 && mirror
->out
== out
263 && mirror
->out_vlan
== out_vlan
264 && mirror
->snaplen
== snaplen
)
266 hmapx_destroy(&srcs_map
);
267 hmapx_destroy(&dsts_map
);
271 /* XXX: Not sure if these need to be thread safe. */
272 hmapx_swap(&srcs_map
, &mirror
->srcs
);
273 hmapx_destroy(&srcs_map
);
275 hmapx_swap(&dsts_map
, &mirror
->dsts
);
276 hmapx_destroy(&dsts_map
);
278 if (vlans
|| src_vlans
) {
279 ovsrcu_postpone(free
, vlans
);
280 vlans
= vlan_bitmap_clone(src_vlans
);
281 ovsrcu_set(&mirror
->vlans
, vlans
);
285 mirror
->out_vlan
= out_vlan
;
286 mirror
->snaplen
= snaplen
;
288 /* Update mbundles. */
289 mirror_bit
= MIRROR_MASK_C(1) << mirror
->idx
;
290 CMAP_FOR_EACH (mbundle
, cmap_node
, &mirror
->mbridge
->mbundles
) {
291 if (hmapx_contains(&mirror
->srcs
, mbundle
)) {
292 mbundle
->src_mirrors
|= mirror_bit
;
294 mbundle
->src_mirrors
&= ~mirror_bit
;
297 if (hmapx_contains(&mirror
->dsts
, mbundle
)) {
298 mbundle
->dst_mirrors
|= mirror_bit
;
300 mbundle
->dst_mirrors
&= ~mirror_bit
;
303 if (mirror
->out
== mbundle
) {
304 mbundle
->mirror_out
|= mirror_bit
;
306 mbundle
->mirror_out
&= ~mirror_bit
;
310 mbridge
->has_mirrors
= true;
311 mirror_update_dups(mbridge
);
317 mirror_destroy(struct mbridge
*mbridge
, void *aux
)
319 struct mirror
*mirror
= mirror_lookup(mbridge
, aux
);
320 mirror_mask_t mirror_bit
;
321 struct mbundle
*mbundle
;
328 mirror_bit
= MIRROR_MASK_C(1) << mirror
->idx
;
329 CMAP_FOR_EACH (mbundle
, cmap_node
, &mbridge
->mbundles
) {
330 mbundle
->src_mirrors
&= ~mirror_bit
;
331 mbundle
->dst_mirrors
&= ~mirror_bit
;
332 mbundle
->mirror_out
&= ~mirror_bit
;
335 hmapx_destroy(&mirror
->srcs
);
336 hmapx_destroy(&mirror
->dsts
);
338 unsigned long *vlans
= ovsrcu_get(unsigned long *, &mirror
->vlans
);
340 ovsrcu_postpone(free
, vlans
);
343 mbridge
->mirrors
[mirror
->idx
] = NULL
;
344 /* mirror_get() might have just read the pointer, so we must postpone the
346 ovsrcu_postpone(free
, mirror
);
348 mirror_update_dups(mbridge
);
350 mbridge
->has_mirrors
= false;
351 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
352 if (mbridge
->mirrors
[i
]) {
353 mbridge
->has_mirrors
= true;
360 mirror_get_stats(struct mbridge
*mbridge
, void *aux
, uint64_t *packets
,
363 struct mirror
*mirror
= mirror_lookup(mbridge
, aux
);
366 *packets
= *bytes
= UINT64_MAX
;
370 *packets
= mirror
->packet_count
;
371 *bytes
= mirror
->byte_count
;
377 mirror_update_stats(struct mbridge
*mbridge
, mirror_mask_t mirrors
,
378 uint64_t packets
, uint64_t bytes
)
380 if (!mbridge
|| !mirrors
) {
384 for (; mirrors
; mirrors
= zero_rightmost_1bit(mirrors
)) {
387 m
= mbridge
->mirrors
[raw_ctz(mirrors
)];
390 /* In normal circumstances 'm' will not be NULL. However, if
391 * mirrors are reconfigured, we can temporarily get out of sync.
392 * We could "correct" the mirror list before reaching here, but
393 * doing that would not properly account the traffic stats we've
394 * currently accumulated for previous mirror configuration. */
398 /* XXX: This is not thread safe, yet we are calling these from the
399 * handler and revalidation threads. But then, maybe these stats do
400 * not need to be very accurate. */
401 m
->packet_count
+= packets
;
402 m
->byte_count
+= bytes
;
406 /* Retrieves the mirror numbered 'index' in 'mbridge'. Returns true if such a
407 * mirror exists, false otherwise.
409 * If successful, '*vlans' receives the mirror's VLAN membership information,
410 * either a null pointer if the mirror includes all VLANs or a 4096-bit bitmap
411 * in which a 1-bit indicates that the mirror includes a particular VLAN,
412 * '*dup_mirrors' receives a bitmap of mirrors whose output duplicates mirror
413 * 'index', '*out' receives the output ofbundle (if any), and '*out_vlan'
414 * receives the output VLAN (if any).
416 * Everything returned here is assumed to be RCU protected.
419 mirror_get(struct mbridge
*mbridge
, int index
, const unsigned long **vlans
,
420 mirror_mask_t
*dup_mirrors
, struct ofbundle
**out
,
421 int *snaplen
, int *out_vlan
)
423 struct mirror
*mirror
;
429 mirror
= mbridge
->mirrors
[index
];
433 /* Assume 'mirror' is RCU protected, i.e., it will not be freed until this
434 * thread quiesces. */
436 *vlans
= ovsrcu_get(unsigned long *, &mirror
->vlans
);
437 *dup_mirrors
= mirror
->dup_mirrors
;
438 *out
= mirror
->out
? mirror
->out
->ofbundle
: NULL
;
439 *out_vlan
= mirror
->out_vlan
;
440 *snaplen
= mirror
->snaplen
;
446 static struct mbundle
*
447 mbundle_lookup(const struct mbridge
*mbridge
, struct ofbundle
*ofbundle
)
449 struct mbundle
*mbundle
;
450 uint32_t hash
= hash_pointer(ofbundle
, 0);
452 CMAP_FOR_EACH_WITH_HASH (mbundle
, cmap_node
, hash
, &mbridge
->mbundles
) {
453 if (mbundle
->ofbundle
== ofbundle
) {
460 /* Looks up each of the 'n_ofbundles' pointers in 'ofbundles' as mbundles and
461 * adds the ones that are found to 'mbundles'. */
463 mbundle_lookup_multiple(const struct mbridge
*mbridge
,
464 struct ofbundle
**ofbundles
, size_t n_ofbundles
,
465 struct hmapx
*mbundles
)
469 hmapx_init(mbundles
);
470 for (i
= 0; i
< n_ofbundles
; i
++) {
471 struct mbundle
*mbundle
= mbundle_lookup(mbridge
, ofbundles
[i
]);
473 hmapx_add(mbundles
, mbundle
);
479 mirror_scan(struct mbridge
*mbridge
)
483 for (idx
= 0; idx
< MAX_MIRRORS
; idx
++) {
484 if (!mbridge
->mirrors
[idx
]) {
491 static struct mirror
*
492 mirror_lookup(struct mbridge
*mbridge
, void *aux
)
496 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
497 struct mirror
*mirror
= mbridge
->mirrors
[i
];
498 if (mirror
&& mirror
->aux
== aux
) {
506 /* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
508 mirror_update_dups(struct mbridge
*mbridge
)
512 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
513 struct mirror
*m
= mbridge
->mirrors
[i
];
516 m
->dup_mirrors
= MIRROR_MASK_C(1) << i
;
520 for (i
= 0; i
< MAX_MIRRORS
; i
++) {
521 struct mirror
*m1
= mbridge
->mirrors
[i
];
528 for (j
= i
+ 1; j
< MAX_MIRRORS
; j
++) {
529 struct mirror
*m2
= mbridge
->mirrors
[j
];
531 if (m2
&& m1
->out
== m2
->out
&& m1
->out_vlan
== m2
->out_vlan
) {
532 m1
->dup_mirrors
|= MIRROR_MASK_C(1) << j
;
533 m2
->dup_mirrors
|= m1
->dup_mirrors
;