/
vr_mirror.c
537 lines (427 loc) · 12.5 KB
/
vr_mirror.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "vr_os.h"
#include "vr_types.h"
#include "vr_packet.h"
#include "vr_interface.h"
#include "vr_sandesh.h"
#include "vr_message.h"
#include "vr_mirror.h"
int vr_mirror_add(vr_mirror_req *);
int vr_mirror_del(vr_mirror_req *);
static struct vr_mirror_entry *
__vrouter_get_mirror(unsigned int rid, unsigned int index)
{
struct vrouter *router = vrouter_get(rid);
if (!router || index >= router->vr_max_mirror_indices)
return NULL;
return router->vr_mirrors[index];
}
struct vr_mirror_entry *
vrouter_get_mirror(unsigned int rid, unsigned int index)
{
struct vr_mirror_entry *mirror;
mirror = __vrouter_get_mirror(rid, index);
if (mirror)
mirror->mir_users++;
return mirror;
}
int
vrouter_put_mirror(struct vrouter *router, unsigned int index)
{
struct vr_mirror_entry *mirror;
if (index >= router->vr_max_mirror_indices)
return -EINVAL;
mirror = router->vr_mirrors[index];
if (!mirror)
return -EINVAL;
if (!--mirror->mir_users) {
router->vr_mirrors[index] = NULL;
if (!vr_not_ready)
vr_delay_op();
vrouter_put_nexthop(mirror->mir_nh);
vr_free(mirror, VR_MIRROR_OBJECT);
}
return 0;
}
int
vr_mirror_del(vr_mirror_req *req)
{
int ret = 0;
struct vrouter *router;
struct vr_mirror_entry *mirror;
struct vr_nexthop *nh;
router = vrouter_get(req->mirr_rid);
if (!router) {
ret = -EINVAL;
goto generate_resp;
}
mirror = __vrouter_get_mirror(req->mirr_rid, req->mirr_index);
if (!mirror) {
ret = -EINVAL;
goto generate_resp;
}
mirror->mir_flags |= VR_MIRROR_FLAG_MARKED_DELETE;
nh = mirror->mir_nh;
mirror->mir_nh = vrouter_get_nexthop(req->mirr_rid, NH_DISCARD_ID);
/* release the old nexthop */
vrouter_put_nexthop(nh);
/* ...and finally try to release the mirror entry */
vrouter_put_mirror(router, req->mirr_index);
generate_resp:
vr_send_response(ret);
return ret;
}
static int
vr_mirror_change(struct vr_mirror_entry *mirror, vr_mirror_req *req,
struct vr_nexthop *nh_new)
{
struct vr_nexthop *nh_old = mirror->mir_nh;
if (mirror->mir_flags & VR_MIRROR_FLAG_MARKED_DELETE) {
mirror->mir_flags &= ~VR_MIRROR_FLAG_MARKED_DELETE;
mirror->mir_users++;
}
mirror->mir_flags |= req->mirr_flags;
mirror->mir_nh = nh_new;
vrouter_put_nexthop(nh_old);
return 0;
}
int
vr_mirror_add(vr_mirror_req *req)
{
struct vrouter *router;
struct vr_nexthop *nh;
int ret = 0;
struct vr_mirror_entry *mirror;
router = vrouter_get(req->mirr_rid);
if (!router) {
ret = -EINVAL;
goto generate_resp;
}
if ((unsigned int)req->mirr_index >= router->vr_max_mirror_indices) {
ret = -EINVAL;
goto generate_resp;
}
req->mirr_flags &= ~VR_MIRROR_FLAG_MARKED_DELETE;
nh = vrouter_get_nexthop(req->mirr_rid, req->mirr_nhid);
if (!nh) {
ret = -EINVAL;
goto generate_resp;
}
mirror = __vrouter_get_mirror(req->mirr_rid, req->mirr_index);
if (mirror) {
vr_mirror_change(mirror, req, nh);
} else {
mirror = vr_zalloc(sizeof(*mirror), VR_MIRROR_OBJECT);
mirror->mir_users++;
mirror->mir_nh = nh;
mirror->mir_rid = req->mirr_rid;
mirror->mir_flags = req->mirr_flags;
router->vr_mirrors[req->mirr_index] = mirror;
}
generate_resp:
vr_send_response(ret);
return ret;
}
static void
vr_mirror_make_req(vr_mirror_req *req, struct vr_mirror_entry *mirror,
unsigned short index)
{
req->mirr_index = index;
if (mirror->mir_nh)
req->mirr_nhid = mirror->mir_nh->nh_id;
req->mirr_users = mirror->mir_users;
req->mirr_flags = mirror->mir_flags;
req->mirr_rid = mirror->mir_rid;
return;
}
static int
vr_mirror_dump(vr_mirror_req *r)
{
int ret = 0;
unsigned int i;
struct vrouter *router = vrouter_get(r->mirr_rid);
vr_mirror_req req;
struct vr_mirror_entry *mirror;
struct vr_message_dumper *dumper = NULL;
if (!router && (ret = -ENODEV))
goto generate_response;
if ((unsigned int)(r->mirr_marker + 1) >= router->vr_max_mirror_indices)
goto generate_response;
dumper = vr_message_dump_init(r);
if (!dumper && (ret = -ENOMEM))
goto generate_response;
for (i = (unsigned int)(r->mirr_marker + 1);
i < router->vr_max_mirror_indices; i++) {
mirror = router->vr_mirrors[i];
if (mirror) {
vr_mirror_make_req(&req, mirror, i);
ret = vr_message_dump_object(dumper, VR_MIRROR_OBJECT_ID, &req);
if (ret <= 0)
break;
}
}
generate_response:
vr_message_dump_exit(dumper, ret);
return 0;
}
static int
vr_mirror_get(vr_mirror_req *req)
{
int ret = 0;
struct vrouter *router;
struct vr_mirror_entry *mirror = NULL;
router = vrouter_get(req->mirr_rid);
if (!router ||
(unsigned int)req->mirr_index >= router->vr_max_mirror_indices) {
ret = -ENODEV;
} else {
mirror = __vrouter_get_mirror(req->mirr_rid, req->mirr_index);
if (!mirror)
ret = -ENOENT;
}
if (mirror) {
vr_mirror_make_req(req, mirror, req->mirr_index);
} else
req = NULL;
return vr_message_response(VR_MIRROR_OBJECT_ID, req, ret);
}
void
vr_mirror_req_process(void *s_req)
{
vr_mirror_req *req = (vr_mirror_req *)s_req;
switch (req->h_op) {
case SANDESH_OP_ADD:
vr_mirror_add(req);
break;
case SANDESH_OP_GET:
vr_mirror_get(req);
break;
case SANDESH_OP_DUMP:
vr_mirror_dump(req);
break;
case SANDESH_OP_DELETE:
vr_mirror_del(req);
break;
default:
break;
}
return;
}
static void
vr_mirror_meta_destroy(struct vr_mirror_meta_entry *me)
{
if (!me)
return;
if (me->mirror_md)
vr_free(me->mirror_md, VR_MIRROR_META_OBJECT);
vr_free(me, VR_MIRROR_META_OBJECT);
return;
}
static void
vr_mirror_meta_destructor(struct vrouter *router, void *arg)
{
struct vr_defer_data *defer = (struct vr_defer_data *)arg;
struct vr_mirror_meta_entry *me;
if (!defer)
return;
me = (struct vr_mirror_meta_entry *)defer->vdd_data;
vr_mirror_meta_destroy(me);
return;
}
static void
vr_mirror_meta_entry_destroy(struct vrouter *router,
struct vr_mirror_meta_entry *me)
{
struct vr_defer_data *defer;
if (me) {
if (!vr_not_ready) {
defer = vr_get_defer_data(sizeof(*defer));
if (!defer) {
vr_delay_op();
vr_mirror_meta_destroy(me);
return;
}
defer->vdd_data = (void *)me;
vr_defer(me->mirror_router, vr_mirror_meta_destructor, (void *)defer);
} else {
vr_mirror_meta_destroy(me);
}
}
return;
}
struct vr_mirror_meta_entry *
vr_mirror_meta_entry_set(struct vrouter *router, unsigned int index,
unsigned int mir_sip, unsigned short mir_sport,
void *meta_data, unsigned int meta_data_len,
unsigned short mirror_vrf)
{
char *buf;
struct vr_mirror_meta_entry *me;
me = vr_malloc(sizeof(*me), VR_MIRROR_META_OBJECT);
if (!me)
return NULL;
buf = vr_malloc(meta_data_len, VR_MIRROR_META_OBJECT);
if (!buf) {
vr_free(me, VR_MIRROR_META_OBJECT);
return NULL;
}
memcpy(buf, meta_data, meta_data_len);
me->mirror_router = router;
me->mirror_md = buf;
me->mirror_md_len = meta_data_len;
me->mirror_sip = mir_sip;
me->mirror_sport = mir_sport;
me->mirror_vrf = mirror_vrf;
return me;
}
void
vr_mirror_meta_entry_del(struct vrouter *router,
struct vr_mirror_meta_entry *me)
{
if (me)
vr_mirror_meta_entry_destroy(router, (void *)me);
return;
}
static struct vr_mirror_meta_entry *
vr_mirror_meta_entry_get(struct vrouter *router, unsigned int flow_index)
{
struct vr_flow_entry *fe;
fe = vr_get_flow_entry(router, flow_index);
if (fe)
return fe->fe_mme;
return NULL;
}
int
vr_mirror(struct vrouter *router, uint8_t mirror_id,
struct vr_packet *pkt, struct vr_forwarding_md *fmd)
{
bool reset = true;
unsigned int captured_len, clone_len = VR_MIRROR_PKT_HEAD_SPACE,
mirror_md_len = 0;
unsigned char default_mme[2] = {0xff, 0x0};
unsigned long sec, usec;
void *mirror_md;
unsigned char *buf;
struct vr_nexthop *nh;
struct vr_pcap *pcap;
struct vr_mirror_entry *mirror;
struct vr_mirror_meta_entry *mme;
struct vr_nexthop *pkt_nh;
struct vr_forwarding_md new_fmd;
/* If the packet is already mirrored, dont mirror again */
if (pkt->vp_flags & VP_FLAG_FROM_DP)
return 0;
mirror = router->vr_mirrors[mirror_id];
if (!mirror)
return 0;
if (fmd) {
memcpy(&new_fmd, fmd, sizeof(*fmd));
fmd = &new_fmd;
}
if (fmd->fmd_flow_index >= 0) {
mme = vr_mirror_meta_entry_get(router, fmd->fmd_flow_index);
if (!mme)
return 0;
mirror_md_len = mme->mirror_md_len;
mirror_md = mme->mirror_md;
fmd->fmd_dvrf = mme->mirror_vrf;
} else {
mirror_md_len = sizeof(default_mme);
mirror_md = default_mme;
}
clone_len += mirror_md_len;
nh = mirror->mir_nh;
pkt->vp_flags &= ~VP_FLAG_GRO;
pkt = vr_pclone(pkt);
if (!pkt)
return 0;
/* If packet is from fabric, mirror it by adding the required L2
* header. If not get the processed headers by resetting the packet
* and mirror it
*/
if (pkt->vp_if && (pkt->vp_if->vif_type == VIF_TYPE_PHYSICAL)) {
pkt_nh = pkt->vp_nh;
if (pkt_nh && (pkt_nh->nh_flags & NH_FLAG_VALID) &&
(pkt_nh->nh_type == NH_ENCAP)) {
reset = false;
if (fmd->fmd_flow_index >= 0) {
if (pkt_nh->nh_family == AF_INET)
clone_len += pkt_nh->nh_encap_len;
if (vr_pcow(pkt, clone_len))
goto fail;
clone_len = 0;
if (pkt_nh->nh_family == AF_INET) {
if (!pkt_nh->nh_dev->vif_set_rewrite(pkt_nh->nh_dev, pkt, fmd,
pkt_nh->nh_data, pkt_nh->nh_encap_len))
goto fail;
}
}
}
}
if (reset)
vr_preset(pkt);
if (clone_len) {
if (vr_pcow(pkt, clone_len))
goto fail;
}
pkt->vp_flags |= VP_FLAG_FROM_DP;
/* Set the GSO and partial checksum flag */
pkt->vp_flags |= (VP_FLAG_FLOW_SET | VP_FLAG_GSO | VP_FLAG_CSUM_PARTIAL);
pkt->vp_flags &= ~VP_FLAG_GRO;
buf = pkt_push(pkt, mirror_md_len);
if (!buf)
goto fail;
captured_len = htonl(pkt_len(pkt));
if (mirror_md_len)
memcpy(buf, mirror_md, mirror_md_len);
if (mirror->mir_flags & VR_MIRROR_PCAP) {
/* Add the pcap header */
pcap = (struct vr_pcap *)pkt_push(pkt, sizeof(struct vr_pcap));
if (!pcap)
goto fail;
pcap->pcap_incl_len = captured_len;
pcap->pcap_orig_len = captured_len;
vr_get_time(&sec, &usec);
pcap->pcap_ts_sec = sec;
pcap->pcap_ts_usec = usec;
pcap->pcap_ts_sec = htonl(pcap->pcap_ts_sec);
pcap->pcap_ts_usec = htonl(pcap->pcap_ts_usec);
}
if (nh->nh_vrf >= 0)
fmd->fmd_dvrf = nh->nh_vrf;
nh_output(pkt, nh, fmd);
return 0;
fail:
vr_pfree(pkt, VP_DROP_PUSH);
return 0;
}
void
vr_mirror_exit(struct vrouter *router, bool soft_reset)
{
unsigned int i;
if (router->vr_mirrors)
for (i = 0; i < router->vr_max_mirror_indices; i++)
if (router->vr_mirrors[i])
vrouter_put_mirror(router, i);
if (!soft_reset) {
vr_free(router->vr_mirrors, VR_MIRROR_TABLE_OBJECT);
router->vr_mirrors = NULL;
router->vr_max_mirror_indices = 0;
}
return;
}
int
vr_mirror_init(struct vrouter *router)
{
unsigned int size;
if (!router->vr_mirrors) {
router->vr_max_mirror_indices = VR_MAX_MIRROR_INDICES;
size = sizeof(struct vr_mirror_entry *) * router->vr_max_mirror_indices;
router->vr_mirrors = vr_zalloc(size, VR_MIRROR_TABLE_OBJECT);
if (!router->vr_mirrors)
return vr_module_error(-ENOMEM, __FUNCTION__, __LINE__, size);
}
return 0;
}