aboutsummaryrefslogtreecommitdiffstats
path: root/vpp-api/java/japi/vppjni_bridge_domain.h
blob: b614a5becb5eef1874d6c7e8c8add26d4c1267b2 (plain)
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
/*---------------------------------------------------------------------------
 * Copyright (c) 2009-2014 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *---------------------------------------------------------------------------
 */

#ifndef __included_vppjni_bridge_domain_h__
#define __included_vppjni_bridge_domain_h__

#include <vlib/vlib.h>
#include <vppinfra/clib.h>
#include <vppinfra/mhash.h>

/*
 * The L2fib key is the mac address and bridge domain ID
 */
#define MAC_ADDRESS_SIZE  6

typedef struct {
  union {
    struct {
      u16 unused1;
      u8  mac[MAC_ADDRESS_SIZE]; 
    } fields;
    u64 raw;
  };
} l2fib_u64_mac_t;

/* 
 * The l2fib entry results
 */
typedef struct {
  u32 bd_id;
  l2fib_u64_mac_t mac_addr;
  u32 sw_if_index;
  u8  learned:1;
  u8  bvi:1;
  u8  filter:1;      // drop packets to/from this mac
  u8  unused1:5;
} bd_l2fib_oper_t;

typedef struct {
  u32 bd_id;
  u8 * bd_name;
} bd_local_cfg_t;

typedef struct {
  u32 bd_id;
  u32 sw_if_index;
  u32 shg;
} bd_sw_if_oper_t;

typedef struct {
  u32 bd_id;
  u8 flood:1;
  u8 forward:1;
  u8 learn:1;
  u8 uu_flood:1;
  u8 arp_term:1;
  u8 unused1:3;
  u32 bvi_sw_if_index;
  u32 n_sw_ifs;
  bd_sw_if_oper_t * bd_sw_if_oper;
  f64 last_sync_time;
  mhash_t l2fib_index_by_mac;
  bd_l2fib_oper_t * l2fib_oper;	// vector indexed by l2fib_index
} vjbd_oper_t;

#define BD_OPER_REFRESH_INTERVAL  	2.0
#define BD_OPER_L2FIB_REFRESH_INTERVAL	5.0

typedef struct {
  u32 next_bd_id;
  uword * bd_index_bitmap;
  uword * bd_index_by_id;
  mhash_t bd_id_by_name;
  bd_local_cfg_t * local_cfg;		// vector indexed by bd_index
  vjbd_oper_t * bd_oper;		// vector indexed by oper_bd_index
  f64 bd_oper_last_sync_all_time;
  bd_sw_if_oper_t * sw_if_oper;	// vector indexed by sw_if_index
  f64 l2fib_oper_last_sync_time;
  uword * bd_id_by_sw_if_index;
  uword * oper_bd_index_by_bd_id;
} vjbd_main_t;

extern vjbd_main_t vjbd_main;

always_inline
u64 l2fib_mac_to_u64 (u8 * mac_address) {
  u64 temp;

  // The mac address in memory is A:B:C:D:E:F
  // The bd id in register is H:L
#if CLIB_ARCH_IS_LITTLE_ENDIAN
  // Create the in-register key as F:E:D:C:B:A:H:L
  // In memory the key is L:H:A:B:C:D:E:F
  temp = *((u64 *)(mac_address - 2));
  temp = (temp & ~0xffff);
#else
  // Create the in-register key as H:L:A:B:C:D:E:F
  // In memory the key is H:L:A:B:C:D:E:F
  temp = *((u64 *)(mac_address)) >> 16;
#endif

  return temp;
}

static_always_inline void vjbd_main_init (vjbd_main_t *bdm)
{
  bdm->bd_index_by_id = hash_create (0, sizeof(uword));
  mhash_init_vec_string (&bdm->bd_id_by_name, sizeof (u32));
  bdm->bd_id_by_sw_if_index = hash_create (0, sizeof (u32));
  bdm->oper_bd_index_by_bd_id = hash_create (0, sizeof (u32));
}

static_always_inline u32 vjbd_id_is_valid (vjbd_main_t * bdm, u32 bd_id)
{
  return ((bd_id != 0) && (bd_id != ~0) && (bd_id <= bdm->next_bd_id));
}

static_always_inline u32 vjbd_index_is_free (vjbd_main_t * bdm, u16 bd_index)
{
  u32 bd_id = vec_elt_at_index(bdm->local_cfg, bd_index)->bd_id;

  return (!clib_bitmap_get (bdm->bd_index_bitmap, (bd_index)) &&
          (bd_index < vec_len (bdm->local_cfg)) &&
          ((bd_id == 0) || (bd_id == ~0)));
}

static_always_inline u32 vjbd_index_is_valid (vjbd_main_t * bdm, u16 bd_index)
{
  return (clib_bitmap_get (bdm->bd_index_bitmap, bd_index) &&
          (bd_index < vec_len (bdm->local_cfg)));
}

static_always_inline u32 vjbd_id_from_name (vjbd_main_t * bdm,
                                            const u8 * bd_name)
{
  u32 bd_id;
  uword * p;

  ASSERT (vec_c_string_is_terminated (bd_name));

  if (bdm->next_bd_id == 0)
    return ~0;

  p = mhash_get (&bdm->bd_id_by_name, (void *)bd_name);
  if (p)
    {
      bd_id = p[0];
      ASSERT (vjbd_id_is_valid (bdm, bd_id));
    }
  else
    bd_id = ~0;

  return bd_id;
}

static_always_inline u32 vjbd_index_from_id (vjbd_main_t * bdm, u32 bd_id)
{
  uword * p;
  u16 bd_index;

  ASSERT (vjbd_id_is_valid (bdm, bd_id));

  p = hash_get (bdm->bd_index_by_id, bd_id);

  ASSERT (p); // there is always an index associated with a valid bd_id
  bd_index = p[0];

  ASSERT (vjbd_index_is_valid (bdm, bd_index));

  return bd_index;
}

static_always_inline u32 vjbd_id_from_index (vjbd_main_t * bdm, u16 bd_index)
{
  u32 bd_id;

  ASSERT (vjbd_index_is_valid (bdm, bd_index));

  bd_id = vec_elt_at_index(bdm->local_cfg, bd_index)->bd_id;

  ASSERT (vjbd_id_is_valid (bdm, bd_id));

  return bd_id;
}

static_always_inline u8 * vjbd_name_from_id (vjbd_main_t * bdm, u32 bd_id)
{
  u16 bd_index = vjbd_index_from_id (bdm, bd_id);

  return vec_elt_at_index(bdm->local_cfg, bd_index)->bd_name;
}

static_always_inline u8 * vjbd_oper_name_from_id (vjbd_main_t * bdm, u32 bd_id)
{
    if (vjbd_id_is_valid (bdm, bd_id)) {
        return format(0, "%s", vjbd_name_from_id(bdm, bd_id));
    } else {
        return format(0, "BridgeDomainOper%d", bd_id);
    }
}

static_always_inline vjbd_oper_t * vjbd_oper_from_id (vjbd_main_t * bdm,
                                                  u32 bd_id)
{
  u16 bd_index = vjbd_index_from_id (bdm, bd_id);
  return vec_elt_at_index (bdm->bd_oper, bd_index);
}

static_always_inline void vjbd_oper_maybe_sync_from_vpp (vjbd_main_t * bdm, 
                                                       u32 bd_id)
{
#ifdef VPPJNI_OPER
  vppjni_vpe_api_msg_main_t *ovam = ovam_get_main ();

  if (bd_id == ~0)
    {
      if ((ovam_time_now (ovam) - bdm->bd_oper_last_sync_all_time) >
          BD_OPER_REFRESH_INTERVAL)
        {
          ovam_bridge_domain_dump (bd_id);
          bdm->bd_oper_last_sync_all_time = ovam_time_now (ovam);
        }
    }
    
  else 
    {
      vjbd_oper_t * bd_oper = vjbd_oper_from_id (bdm, bd_id);

      if ((ovam_time_now (ovam) - bd_oper->last_sync_time) >
          BD_OPER_REFRESH_INTERVAL)
        {
          ovam_bridge_domain_dump (bd_id);

          bd_oper->last_sync_time = ovam_time_now (ovam);
        }
    }
#endif
}

static_always_inline u32 vjbd_id_from_sw_if_index (vjbd_main_t * bdm,
                                                 u32 sw_if_index)
{
  bd_sw_if_oper_t * bd_sw_if_oper;
  u32 bd_id = ~0;

  vjbd_oper_maybe_sync_from_vpp (bdm, ~0);
  if (sw_if_index < vec_len (bdm->sw_if_oper))
    {
      bd_sw_if_oper = vec_elt_at_index (bdm->sw_if_oper, sw_if_index);
      bd_id = bd_sw_if_oper->bd_id;
    }

  return bd_id;
}

static_always_inline u8 * vjbd_name_from_sw_if_index (vjbd_main_t * bdm,
                                                    u32 sw_if_index)
{
  u32 bd_id, bd_index;
  u8 * bd_name = 0;
  
  /* DAW-FIXME:
  ASSERT (ovam_sw_if_index_valid (ovam_get_main(), sw_if_index));
  */
  vjbd_oper_maybe_sync_from_vpp (bdm, ~0);
  bd_id = vjbd_id_from_sw_if_index (bdm, sw_if_index);
  if (vjbd_id_is_valid (bdm, bd_id))
    {
      bd_index = vjbd_index_from_id (bdm, bd_id);
      bd_name = vec_elt_at_index (bdm->local_cfg, bd_index)->bd_name;
    }

  return bd_name;
}

static_always_inline u32 
vjbd_oper_l2fib_index_from_mac (vjbd_oper_t * bd_oper, u8 * mac)
{
  u32 l2fib_index;
  uword * p;

  p = mhash_get (&bd_oper->l2fib_index_by_mac, mac);
  if (p)
    {
      l2fib_index = p[0];
      ASSERT (l2fib_index < vec_len (bd_oper->l2fib_oper));
    }
  else
    l2fib_index = ~0;

  return l2fib_index;
}

static_always_inline u32 vjbd_local_cfg_next_id (vjbd_main_t * bdm, 
                                               u32 bd_id)
{
  u32 i, end = vec_len (bdm->local_cfg);
  u32 next_bd_id = 0;

  if ((bd_id == 0) || vjbd_id_is_valid (bdm, bd_id))
    for (i = 0; i < end; i++)
      {
        u32 curr_bd_id = bdm->local_cfg[i].bd_id;
        if ((curr_bd_id != ~0) && (curr_bd_id > bd_id) && 
            ((next_bd_id == 0) || (curr_bd_id < next_bd_id)))
          next_bd_id = curr_bd_id;
      }

  return next_bd_id;
}

static_always_inline u32 vjbd_sw_if_oper_next_index (vjbd_main_t * bdm, 
                                                   u32 start, u32 bd_id)
{
  u32 i, end = vec_len (bdm->sw_if_oper);

  if (vjbd_id_is_valid (bdm, bd_id))
    for (i = start; i < end; i++)
      if (bdm->sw_if_oper[i].bd_id == bd_id)
        return i;

  return ~0;
}

static_always_inline void 
vjbd_oper_l2fib_maybe_sync_from_vpp (vjbd_main_t * bdm)
{
#ifdef VPPJNI_OPER
  vppjni_vpe_api_msg_main_t *ovam = ovam_get_main ();
  if ((ovam_time_now (ovam) - bdm->l2fib_oper_last_sync_time) >
      BD_OPER_L2FIB_REFRESH_INTERVAL)
    {
      ovam_l2fib_table_dump ();
      bdm->l2fib_oper_last_sync_time = ovam_time_now (ovam);
    }
#endif
}

static_always_inline void vjbd_l2fib_oper_reset (vjbd_main_t * bdm)
{
  vjbd_oper_t * bd_oper;

  vec_foreach (bd_oper, bdm->bd_oper)
    {
      mhash_init (&bd_oper->l2fib_index_by_mac, sizeof (u32), MAC_ADDRESS_SIZE);
      vec_reset_length (bd_oper->l2fib_oper);      
    }
}

static_always_inline void vjbd_oper_reset (vjbd_main_t * bdm, u32 bd_id)
{
  u16 bd_index;
  u32 si, len;
  vjbd_oper_t * bd_oper;
  u32 end;

  if (!bdm->bd_oper)
    {
      ASSERT (vec_len (bdm->sw_if_oper) == 0);
      return;
    }

  if (bd_id == ~0)
    {
      bdm->bd_oper_last_sync_all_time = 0.0;
      bd_index = 0;
      end = vec_len (bdm->bd_oper);
    }
  else
    {
      bd_index = vjbd_index_from_id (bdm, bd_id);
      end = bd_index + 1;
    }

  for (; bd_index < end; bd_index++)
    {
      bd_oper = vec_elt_at_index (bdm->bd_oper, bd_index);
      bd_oper->last_sync_time = 0.0;

      len = vec_len (bdm->sw_if_oper);
      for (si = vjbd_sw_if_oper_next_index (bdm, 0, bd_id);
           (si != ~0) && (si < len);
           si = vjbd_sw_if_oper_next_index (bdm, si + 1, bd_id))
        {
          bd_sw_if_oper_t * bd_sw_if_oper;

          bd_sw_if_oper = vec_elt_at_index (bdm->sw_if_oper, si);
          bd_sw_if_oper->bd_id = ~0;
        }
    }
}

static_always_inline void 
vjbd_sw_if_add_del (u32 sw_if_index ,u32 bd_id, u8 bvi, u8 shg, u8 is_add)
{
  vjbd_main_t * bdm = &vjbd_main;
  u16 bd_index = vjbd_index_from_id (bdm, bd_id);
  vjbd_oper_t * bd_oper = vec_elt_at_index (bdm->bd_oper, bd_index);
  bd_sw_if_oper_t * bd_sw_if_oper;
  
  ASSERT (vjbd_id_is_valid (bdm, bd_id));
  /* DAW-FIXME
  ASSERT (ovam_sw_if_index_valid (ovam_get_main (), sw_if_index));
  */

  vec_validate (bdm->sw_if_oper, sw_if_index);
  bd_sw_if_oper = vec_elt_at_index (bdm->sw_if_oper, sw_if_index);
  if (is_add)
    {
      bd_sw_if_oper->bd_id = bd_id;
      bd_sw_if_oper->shg = shg;
      bd_oper->bvi_sw_if_index = bvi ? sw_if_index : ~0;
    }
  else
    {
      bd_sw_if_oper->bd_id = 0;
      bd_sw_if_oper->shg = 0;
      if (bd_oper->bvi_sw_if_index == sw_if_index)
        bd_oper->bvi_sw_if_index = ~0;
    }
}

static_always_inline u32 vjbd_id_sw_if_count (vjbd_main_t * bdm, u32 bd_id)
{
  u32 count = 0, i, end = vec_len (bdm->sw_if_oper);

  if (vjbd_id_is_valid (bdm, bd_id))
    for (count = i = 0; i < end; i++)
      if (bdm->sw_if_oper[i].bd_id == bd_id)
        count++;

  return count;
}

static_always_inline u32 vjbd_find_or_add_bd (vjbd_main_t * bdm, u8 * bd_name)
{
  u16 bd_index;
  u32 bd_id;
  bd_local_cfg_t * bd_local_cfg;
  uword mhash_val_bd_id;

  bd_id = vjbd_id_from_name (bdm, bd_name);
  if (bd_id != ~0)
    return bd_id;

  mhash_val_bd_id = bd_id = ++bdm->next_bd_id;
  mhash_set_mem (&bdm->bd_id_by_name, (void *)bd_name, &mhash_val_bd_id, 0);

  bd_index = clib_bitmap_first_clear (bdm->bd_index_bitmap);
  vec_validate (bdm->local_cfg, bd_index);
  vec_validate (bdm->bd_oper, bd_index);

  ASSERT (vjbd_index_is_free (bdm, bd_index));

  bd_local_cfg = vec_elt_at_index (bdm->local_cfg, bd_index);
  bd_local_cfg->bd_id = bd_id;
  vec_validate_init_c_string (bd_local_cfg->bd_name, bd_name,
                              vec_len (bd_name) - 1);
  hash_set (bdm->bd_index_by_id, bd_id, bd_index);
  bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap,
                                          bd_index, 1);
  return bd_id;
}

static_always_inline void vjbd_delete_bd (vjbd_main_t * bdm, u32 bd_id)
{
  u16 bd_index;
  bd_local_cfg_t * bd_local_cfg;

  ASSERT (vjbd_id_is_valid (bdm, bd_id));

  // bd must not have any members before deleting
  ASSERT (!vjbd_id_sw_if_count (bdm, bd_id));

  bd_index = vjbd_index_from_id (bdm, bd_id);
  bd_local_cfg = vec_elt_at_index (bdm->local_cfg, bd_index);
  vjbd_oper_reset (bdm, bd_id);

  mhash_unset (&bdm->bd_id_by_name, vjbd_name_from_id (bdm, bd_id), 0);
  bdm->bd_index_bitmap = clib_bitmap_set (bdm->bd_index_bitmap,
                                          bd_index, 0);
  hash_unset (bdm->bd_index_by_id, bd_id);
  bd_local_cfg->bd_id = ~0;
  vec_validate_init_c_string (bd_local_cfg->bd_name, "", 0);

  if (clib_bitmap_is_zero (bdm->bd_index_bitmap))
    {
      vec_reset_length (bdm->local_cfg);
      vec_reset_length (bdm->bd_oper);
    }

  /* Force a resync of all bd_oper data. */
  bdm->bd_oper_last_sync_all_time = 0.0;
  vjbd_oper_maybe_sync_from_vpp (bdm, ~0);
}

#endif /* __included_vppjni_vpp_bridge_domain_h__ */