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
134 * revalidation, and resets the revalidation flag to false. */
136 mbridge_need_revalidate(struct mbridge *mbridge)
138 bool need_revalidate = mbridge->need_revalidate;
139 mbridge->need_revalidate = false;
140 return need_revalidate;
144 mbridge_register_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
146 struct mbundle *mbundle;
148 mbundle = xzalloc(sizeof *mbundle);
149 mbundle->ofbundle = ofbundle;
150 hmap_insert(&mbridge->mbundles, &mbundle->hmap_node,
151 hash_pointer(ofbundle, 0));
155 mbridge_unregister_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
157 struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
164 for (i = 0; i < MAX_MIRRORS; i++) {
165 struct mirror *m = mbridge->mirrors[i];
167 if (m->out == mbundle) {
168 mirror_destroy(mbridge, m->aux);
169 } else if (hmapx_find_and_delete(&m->srcs, mbundle)
170 || hmapx_find_and_delete(&m->dsts, mbundle)) {
171 mbridge->need_revalidate = true;
176 hmap_remove(&mbridge->mbundles, &mbundle->hmap_node);
181 mirror_bundle_out(struct mbridge *mbridge, struct ofbundle *ofbundle)
183 struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
184 return mbundle ? mbundle->mirror_out : 0;
188 mirror_bundle_src(struct mbridge *mbridge, struct ofbundle *ofbundle)
190 struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
191 return mbundle ? mbundle->src_mirrors : 0;
195 mirror_bundle_dst(struct mbridge *mbridge, struct ofbundle *ofbundle)
197 struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
198 return mbundle ? mbundle->dst_mirrors : 0;
202 mirror_set(struct mbridge *mbridge, void *aux, const char *name,
203 struct ofbundle **srcs, size_t n_srcs,
204 struct ofbundle **dsts, size_t n_dsts,
205 unsigned long *src_vlans, struct ofbundle *out_bundle,
208 struct mbundle *mbundle, *out;
209 mirror_mask_t mirror_bit;
210 struct mirror *mirror;
211 struct hmapx srcs_map; /* Contains "struct ofbundle *"s. */
212 struct hmapx dsts_map; /* Contains "struct ofbundle *"s. */
214 mirror = mirror_lookup(mbridge, aux);
218 idx = mirror_scan(mbridge);
220 VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
225 mirror = mbridge->mirrors[idx] = xzalloc(sizeof *mirror);
226 mirror->mbridge = mbridge;
229 mirror->out_vlan = -1;
232 /* Get the new configuration. */
234 out = mbundle_lookup(mbridge, out_bundle);
236 mirror_destroy(mbridge, mirror->aux);
243 mbundle_lookup_multiple(mbridge, srcs, n_srcs, &srcs_map);
244 mbundle_lookup_multiple(mbridge, dsts, n_dsts, &dsts_map);
246 /* If the configuration has not changed, do nothing. */
247 if (hmapx_equals(&srcs_map, &mirror->srcs)
248 && hmapx_equals(&dsts_map, &mirror->dsts)
249 && vlan_bitmap_equal(mirror->vlans, src_vlans)
250 && mirror->out == out
251 && mirror->out_vlan == out_vlan)
253 hmapx_destroy(&srcs_map);
254 hmapx_destroy(&dsts_map);
258 hmapx_swap(&srcs_map, &mirror->srcs);
259 hmapx_destroy(&srcs_map);
261 hmapx_swap(&dsts_map, &mirror->dsts);
262 hmapx_destroy(&dsts_map);
265 mirror->vlans = vlan_bitmap_clone(src_vlans);
268 mirror->out_vlan = out_vlan;
270 /* Update mbundles. */
271 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
272 HMAP_FOR_EACH (mbundle, hmap_node, &mirror->mbridge->mbundles) {
273 if (hmapx_contains(&mirror->srcs, mbundle)) {
274 mbundle->src_mirrors |= mirror_bit;
276 mbundle->src_mirrors &= ~mirror_bit;
279 if (hmapx_contains(&mirror->dsts, mbundle)) {
280 mbundle->dst_mirrors |= mirror_bit;
282 mbundle->dst_mirrors &= ~mirror_bit;
285 if (mirror->out == mbundle) {
286 mbundle->mirror_out |= mirror_bit;
288 mbundle->mirror_out &= ~mirror_bit;
292 mbridge->has_mirrors = true;
293 mirror_update_dups(mbridge);
299 mirror_destroy(struct mbridge *mbridge, void *aux)
301 struct mirror *mirror = mirror_lookup(mbridge, aux);
302 mirror_mask_t mirror_bit;
303 struct mbundle *mbundle;
310 mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
311 HMAP_FOR_EACH (mbundle, hmap_node, &mbridge->mbundles) {
312 mbundle->src_mirrors &= ~mirror_bit;
313 mbundle->dst_mirrors &= ~mirror_bit;
314 mbundle->mirror_out &= ~mirror_bit;
317 hmapx_destroy(&mirror->srcs);
318 hmapx_destroy(&mirror->dsts);
321 mbridge->mirrors[mirror->idx] = NULL;
324 mirror_update_dups(mbridge);
326 mbridge->has_mirrors = false;
327 for (i = 0; i < MAX_MIRRORS; i++) {
328 if (mbridge->mirrors[i]) {
329 mbridge->has_mirrors = true;
336 mirror_get_stats(struct mbridge *mbridge, void *aux, uint64_t *packets,
339 struct mirror *mirror = mirror_lookup(mbridge, aux);
342 *packets = *bytes = UINT64_MAX;
346 *packets = mirror->packet_count;
347 *bytes = mirror->byte_count;
353 mirror_update_stats(struct mbridge *mbridge, mirror_mask_t mirrors,
354 uint64_t packets, uint64_t bytes)
356 if (!mbridge || !mirrors) {
360 for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
363 m = mbridge->mirrors[raw_ctz(mirrors)];
366 /* In normal circumstances 'm' will not be NULL. However,
367 * if mirrors are reconfigured, we can temporarily get out
368 * of sync in facet_revalidate(). We could "correct" the
369 * mirror list before reaching here, but doing that would
370 * not properly account the traffic stats we've currently
371 * accumulated for previous mirror configuration. */
375 m->packet_count += packets;
376 m->byte_count += bytes;
380 /* Retrieves the mirror numbered 'index' in 'mbridge'. Returns true if such a
381 * mirror exists, false otherwise.
383 * If successful, '*vlans' receives the mirror's VLAN membership information,
384 * either a null pointer if the mirror includes all VLANs or a 4096-bit bitmap
385 * in which a 1-bit indicates that the mirror includes a particular VLAN,
386 * '*dup_mirrors' receives a bitmap of mirrors whose output duplicates mirror
387 * 'index', '*out' receives the output ofbundle (if any), and '*out_vlan'
388 * receives the output VLAN (if any). */
390 mirror_get(struct mbridge *mbridge, int index, const unsigned long **vlans,
391 mirror_mask_t *dup_mirrors, struct ofbundle **out, int *out_vlan)
393 struct mirror *mirror;
399 mirror = mbridge->mirrors[index];
404 *vlans = mirror->vlans;
405 *dup_mirrors = mirror->dup_mirrors;
406 *out = mirror->out ? mirror->out->ofbundle : NULL;
407 *out_vlan = mirror->out_vlan;
413 static struct mbundle *
414 mbundle_lookup(const struct mbridge *mbridge, struct ofbundle *ofbundle)
416 struct mbundle *mbundle;
418 HMAP_FOR_EACH_IN_BUCKET (mbundle, hmap_node, hash_pointer(ofbundle, 0),
419 &mbridge->mbundles) {
420 if (mbundle->ofbundle == ofbundle) {
427 /* Looks up each of the 'n_ofbundlees' pointers in 'ofbundlees' as mbundles and
428 * adds the ones that are found to 'mbundles'. */
430 mbundle_lookup_multiple(const struct mbridge *mbridge,
431 struct ofbundle **ofbundles, size_t n_ofbundles,
432 struct hmapx *mbundles)
436 hmapx_init(mbundles);
437 for (i = 0; i < n_ofbundles; i++) {
438 struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundles[i]);
440 hmapx_add(mbundles, mbundle);
446 mirror_scan(struct mbridge *mbridge)
450 for (idx = 0; idx < MAX_MIRRORS; idx++) {
451 if (!mbridge->mirrors[idx]) {
458 static struct mirror *
459 mirror_lookup(struct mbridge *mbridge, void *aux)
463 for (i = 0; i < MAX_MIRRORS; i++) {
464 struct mirror *mirror = mbridge->mirrors[i];
465 if (mirror && mirror->aux == aux) {
473 /* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
475 mirror_update_dups(struct mbridge *mbridge)
479 for (i = 0; i < MAX_MIRRORS; i++) {
480 struct mirror *m = mbridge->mirrors[i];
483 m->dup_mirrors = MIRROR_MASK_C(1) << i;
487 for (i = 0; i < MAX_MIRRORS; i++) {
488 struct mirror *m1 = mbridge->mirrors[i];
495 for (j = i + 1; j < MAX_MIRRORS; j++) {
496 struct mirror *m2 = mbridge->mirrors[j];
498 if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
499 m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
500 m2->dup_mirrors |= m1->dup_mirrors;