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);
132
mbridge_has_mirrors(struct mbridge *mbridge)
134
return mbridge ? mbridge->has_mirrors : false;
137
/* Returns true if configurations changes in 'mbridge''s mirrors require
140
mbridge_need_revalidate(struct mbridge *mbridge)
142
return mbridge->need_revalidate;
146
mbridge_register_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
148
struct mbundle *mbundle;
150
mbundle = xzalloc(sizeof *mbundle);
151
mbundle->ofbundle = ofbundle;
152
hmap_insert(&mbridge->mbundles, &mbundle->hmap_node,
153
hash_pointer(ofbundle, 0));
157
mbridge_unregister_bundle(struct mbridge *mbridge, struct ofbundle *ofbundle)
159
struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
166
for (i = 0; i < MAX_MIRRORS; i++) {
167
struct mirror *m = mbridge->mirrors[i];
169
if (m->out == mbundle) {
170
mirror_destroy(mbridge, m->aux);
171
} else if (hmapx_find_and_delete(&m->srcs, mbundle)
172
|| hmapx_find_and_delete(&m->dsts, mbundle)) {
173
mbridge->need_revalidate = true;
178
hmap_remove(&mbridge->mbundles, &mbundle->hmap_node);
183
mirror_bundle_out(struct mbridge *mbridge, struct ofbundle *ofbundle)
185
struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
186
return mbundle ? mbundle->mirror_out : 0;
190
mirror_bundle_src(struct mbridge *mbridge, struct ofbundle *ofbundle)
192
struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
193
return mbundle ? mbundle->src_mirrors : 0;
197
mirror_bundle_dst(struct mbridge *mbridge, struct ofbundle *ofbundle)
199
struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundle);
200
return mbundle ? mbundle->dst_mirrors : 0;
204
mirror_set(struct mbridge *mbridge, void *aux, const char *name,
205
struct ofbundle **srcs, size_t n_srcs,
206
struct ofbundle **dsts, size_t n_dsts,
207
unsigned long *src_vlans, struct ofbundle *out_bundle,
210
struct mbundle *mbundle, *out;
211
mirror_mask_t mirror_bit;
212
struct mirror *mirror;
213
struct hmapx srcs_map; /* Contains "struct ofbundle *"s. */
214
struct hmapx dsts_map; /* Contains "struct ofbundle *"s. */
216
mirror = mirror_lookup(mbridge, aux);
220
idx = mirror_scan(mbridge);
222
VLOG_WARN("maximum of %d port mirrors reached, cannot create %s",
227
mirror = mbridge->mirrors[idx] = xzalloc(sizeof *mirror);
228
mirror->mbridge = mbridge;
231
mirror->out_vlan = -1;
234
/* Get the new configuration. */
236
out = mbundle_lookup(mbridge, out_bundle);
238
mirror_destroy(mbridge, mirror->aux);
245
mbundle_lookup_multiple(mbridge, srcs, n_srcs, &srcs_map);
246
mbundle_lookup_multiple(mbridge, dsts, n_dsts, &dsts_map);
248
/* If the configuration has not changed, do nothing. */
249
if (hmapx_equals(&srcs_map, &mirror->srcs)
250
&& hmapx_equals(&dsts_map, &mirror->dsts)
251
&& vlan_bitmap_equal(mirror->vlans, src_vlans)
252
&& mirror->out == out
253
&& mirror->out_vlan == out_vlan)
255
hmapx_destroy(&srcs_map);
256
hmapx_destroy(&dsts_map);
260
hmapx_swap(&srcs_map, &mirror->srcs);
261
hmapx_destroy(&srcs_map);
263
hmapx_swap(&dsts_map, &mirror->dsts);
264
hmapx_destroy(&dsts_map);
267
mirror->vlans = vlan_bitmap_clone(src_vlans);
270
mirror->out_vlan = out_vlan;
272
/* Update mbundles. */
273
mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
274
HMAP_FOR_EACH (mbundle, hmap_node, &mirror->mbridge->mbundles) {
275
if (hmapx_contains(&mirror->srcs, mbundle)) {
276
mbundle->src_mirrors |= mirror_bit;
278
mbundle->src_mirrors &= ~mirror_bit;
281
if (hmapx_contains(&mirror->dsts, mbundle)) {
282
mbundle->dst_mirrors |= mirror_bit;
284
mbundle->dst_mirrors &= ~mirror_bit;
287
if (mirror->out == mbundle) {
288
mbundle->mirror_out |= mirror_bit;
290
mbundle->mirror_out &= ~mirror_bit;
294
mbridge->has_mirrors = true;
295
mirror_update_dups(mbridge);
301
mirror_destroy(struct mbridge *mbridge, void *aux)
303
struct mirror *mirror = mirror_lookup(mbridge, aux);
304
mirror_mask_t mirror_bit;
305
struct mbundle *mbundle;
312
mirror_bit = MIRROR_MASK_C(1) << mirror->idx;
313
HMAP_FOR_EACH (mbundle, hmap_node, &mbridge->mbundles) {
314
mbundle->src_mirrors &= ~mirror_bit;
315
mbundle->dst_mirrors &= ~mirror_bit;
316
mbundle->mirror_out &= ~mirror_bit;
319
hmapx_destroy(&mirror->srcs);
320
hmapx_destroy(&mirror->dsts);
323
mbridge->mirrors[mirror->idx] = NULL;
326
mirror_update_dups(mbridge);
328
mbridge->has_mirrors = false;
329
for (i = 0; i < MAX_MIRRORS; i++) {
330
if (mbridge->mirrors[i]) {
331
mbridge->has_mirrors = true;
338
mirror_get_stats(struct mbridge *mbridge, void *aux, uint64_t *packets,
341
struct mirror *mirror = mirror_lookup(mbridge, aux);
344
*packets = *bytes = UINT64_MAX;
348
*packets = mirror->packet_count;
349
*bytes = mirror->byte_count;
355
mirror_update_stats(struct mbridge *mbridge, mirror_mask_t mirrors,
356
uint64_t packets, uint64_t bytes)
358
if (!mbridge || !mirrors) {
362
for (; mirrors; mirrors = zero_rightmost_1bit(mirrors)) {
365
m = mbridge->mirrors[mirror_mask_ffs(mirrors) - 1];
368
/* In normal circumstances 'm' will not be NULL. However,
369
* if mirrors are reconfigured, we can temporarily get out
370
* of sync in facet_revalidate(). We could "correct" the
371
* mirror list before reaching here, but doing that would
372
* not properly account the traffic stats we've currently
373
* accumulated for previous mirror configuration. */
377
m->packet_count += packets;
378
m->byte_count += bytes;
382
/* Retrieves the mirror in 'mbridge' represented by the first bet set of
383
* 'mirrors'. Returns true if such a mirror exists, false otherwise.
384
* The caller takes ownership of, and is expected to deallocate, 'vlans' */
386
mirror_get(struct mbridge *mbridge, int index, unsigned long **vlans,
387
mirror_mask_t *dup_mirrors, struct ofbundle **out, int *out_vlan)
389
struct mirror *mirror;
395
mirror = mbridge->mirrors[index];
400
*vlans = vlan_bitmap_clone(mirror->vlans);
401
*dup_mirrors = mirror->dup_mirrors;
402
*out = mirror->out ? mirror->out->ofbundle : NULL;
403
*out_vlan = mirror->out_vlan;
409
static struct mbundle *
410
mbundle_lookup(const struct mbridge *mbridge, struct ofbundle *ofbundle)
412
struct mbundle *mbundle;
414
HMAP_FOR_EACH_IN_BUCKET (mbundle, hmap_node, hash_pointer(ofbundle, 0),
415
&mbridge->mbundles) {
416
if (mbundle->ofbundle == ofbundle) {
423
/* Looks up each of the 'n_ofbundlees' pointers in 'ofbundlees' as mbundles and
424
* adds the ones that are found to 'mbundles'. */
426
mbundle_lookup_multiple(const struct mbridge *mbridge,
427
struct ofbundle **ofbundles, size_t n_ofbundles,
428
struct hmapx *mbundles)
432
hmapx_init(mbundles);
433
for (i = 0; i < n_ofbundles; i++) {
434
struct mbundle *mbundle = mbundle_lookup(mbridge, ofbundles[i]);
436
hmapx_add(mbundles, mbundle);
442
mirror_scan(struct mbridge *mbridge)
446
for (idx = 0; idx < MAX_MIRRORS; idx++) {
447
if (!mbridge->mirrors[idx]) {
454
static struct mirror *
455
mirror_lookup(struct mbridge *mbridge, void *aux)
459
for (i = 0; i < MAX_MIRRORS; i++) {
460
struct mirror *mirror = mbridge->mirrors[i];
461
if (mirror && mirror->aux == aux) {
469
/* Update the 'dup_mirrors' member of each of the mirrors in 'ofproto'. */
471
mirror_update_dups(struct mbridge *mbridge)
475
for (i = 0; i < MAX_MIRRORS; i++) {
476
struct mirror *m = mbridge->mirrors[i];
479
m->dup_mirrors = MIRROR_MASK_C(1) << i;
483
for (i = 0; i < MAX_MIRRORS; i++) {
484
struct mirror *m1 = mbridge->mirrors[i];
491
for (j = i + 1; j < MAX_MIRRORS; j++) {
492
struct mirror *m2 = mbridge->mirrors[j];
494
if (m2 && m1->out == m2->out && m1->out_vlan == m2->out_vlan) {
495
m1->dup_mirrors |= MIRROR_MASK_C(1) << j;
496
m2->dup_mirrors |= m1->dup_mirrors;