GBP: Endpoints with VLAN tags and birdges that don't learn
[vpp.git] / src / plugins / gbp / gbp_learn.c
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <plugins/gbp/gbp.h>
17 #include <plugins/gbp/gbp_learn.h>
18 #include <plugins/gbp/gbp_bridge_domain.h>
19 #include <vlibmemory/api.h>
20
21 #include <vnet/util/throttle.h>
22 #include <vnet/l2/l2_input.h>
23 #include <vnet/fib/fib_table.h>
24 #include <vnet/vxlan-gbp/vxlan_gbp_packet.h>
25
26 /**
27  * Grouping of global data for the GBP source EPG classification feature
28  */
29 typedef struct gbp_learn_main_t_
30 {
31   /**
32    * Next nodes for L2 output features
33    */
34   u32 gl_l2_input_feat_next[32];
35
36   /**
37    * logger - VLIB log class
38    */
39   vlib_log_class_t gl_logger;
40
41   /**
42    * throttles for the DP leanring
43    */
44   throttle_t gl_l2_throttle;
45   throttle_t gl_l3_throttle;
46 } gbp_learn_main_t;
47
48 /**
49  * The maximum learning rate per-hashed EP
50  */
51 #define GBP_ENDPOINT_HASH_LEARN_RATE (1e-2)
52
53 static gbp_learn_main_t gbp_learn_main;
54
55 #define GBP_LEARN_DBG(...)                                      \
56     vlib_log_debug (gbp_learn_main.gl_logger, __VA_ARGS__);
57
58 #define foreach_gbp_learn                      \
59   _(DROP,    "drop")
60
61 typedef enum
62 {
63 #define _(sym,str) GBP_LEARN_ERROR_##sym,
64   foreach_gbp_learn
65 #undef _
66     GBP_LEARN_N_ERROR,
67 } gbp_learn_error_t;
68
69 static char *gbp_learn_error_strings[] = {
70 #define _(sym,string) string,
71   foreach_gbp_learn
72 #undef _
73 };
74
75 typedef enum
76 {
77 #define _(sym,str) GBP_LEARN_NEXT_##sym,
78   foreach_gbp_learn
79 #undef _
80     GBP_LEARN_N_NEXT,
81 } gbp_learn_next_t;
82
83 typedef struct gbp_learn_l2_t_
84 {
85   ip46_address_t ip;
86   mac_address_t mac;
87   u32 sw_if_index;
88   u32 bd_index;
89   epg_id_t epg;
90   ip46_address_t outer_src;
91   ip46_address_t outer_dst;
92 } gbp_learn_l2_t;
93
94
95 static void
96 gbp_learn_l2_cp (const gbp_learn_l2_t * gl2)
97 {
98   ip46_address_t *ips = NULL;
99
100   GBP_LEARN_DBG ("L2 EP: %U %U, %d",
101                  format_mac_address_t, &gl2->mac,
102                  format_ip46_address, &gl2->ip, IP46_TYPE_ANY, gl2->epg);
103
104   vec_add1 (ips, gl2->ip);
105
106   ASSERT (!ip46_address_is_zero (&gl2->outer_src));
107   ASSERT (!ip46_address_is_zero (&gl2->outer_dst));
108
109   /*
110    * flip the source and dst, since that's how it was received, this API
111    * takes how it's sent
112    */
113   gbp_endpoint_update (gl2->sw_if_index, ips,
114                        &gl2->mac, gl2->epg,
115                        (GBP_ENDPOINT_FLAG_LEARNT |
116                         GBP_ENDPOINT_FLAG_REMOTE),
117                        &gl2->outer_dst, &gl2->outer_src, NULL);
118 }
119
120 static void
121 gbp_learn_l2_ip4_dp (const u8 * mac, const ip4_address_t * ip,
122                      u32 bd_index, u32 sw_if_index, epg_id_t epg,
123                      const ip4_address_t * outer_src,
124                      const ip4_address_t * outer_dst)
125 {
126   gbp_learn_l2_t gl2 = {
127     .sw_if_index = sw_if_index,
128     .bd_index = bd_index,
129     .epg = epg,
130     .ip.ip4 = *ip,
131     .outer_src.ip4 = *outer_src,
132     .outer_dst.ip4 = *outer_dst,
133   };
134   mac_address_from_bytes (&gl2.mac, mac);
135
136   ASSERT (!ip46_address_is_zero (&gl2.outer_src));
137   ASSERT (!ip46_address_is_zero (&gl2.outer_dst));
138
139   vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
140 }
141
142 static void
143 gbp_learn_l2_ip6_dp (const u8 * mac, const ip6_address_t * ip,
144                      u32 bd_index, u32 sw_if_index, epg_id_t epg,
145                      const ip4_address_t * outer_src,
146                      const ip4_address_t * outer_dst)
147 {
148   gbp_learn_l2_t gl2 = {
149     .sw_if_index = sw_if_index,
150     .bd_index = bd_index,
151     .epg = epg,
152     .ip.ip6 = *ip,
153     .outer_src.ip4 = *outer_src,
154     .outer_dst.ip4 = *outer_dst,
155   };
156   mac_address_from_bytes (&gl2.mac, mac);
157
158   vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
159 }
160
161 static void
162 gbp_learn_l2_dp (const u8 * mac, u32 bd_index, u32 sw_if_index,
163                  epg_id_t epg,
164                  const ip4_address_t * outer_src,
165                  const ip4_address_t * outer_dst)
166 {
167   gbp_learn_l2_t gl2 = {
168     .sw_if_index = sw_if_index,
169     .bd_index = bd_index,
170     .epg = epg,
171     .outer_src.ip4 = *outer_src,
172     .outer_dst.ip4 = *outer_dst,
173   };
174   mac_address_from_bytes (&gl2.mac, mac);
175
176   vl_api_rpc_call_main_thread (gbp_learn_l2_cp, (u8 *) & gl2, sizeof (gl2));
177 }
178
179 /**
180  * per-packet trace data
181  */
182 typedef struct gbp_learn_l2_trace_t_
183 {
184   /* per-pkt trace data */
185   mac_address_t mac;
186   u32 sw_if_index;
187   u32 new;
188   u32 throttled;
189   u32 epg;
190   u32 d_bit;
191 } gbp_learn_l2_trace_t;
192
193 always_inline void
194 gbp_learn_get_outer (const ethernet_header_t * eh0,
195                      ip4_address_t * outer_src, ip4_address_t * outer_dst)
196 {
197   ip4_header_t *ip0;
198   u8 *buff;
199
200   /* rewind back to the ivxlan header */
201   buff = (u8 *) eh0;
202   buff -= (sizeof (vxlan_gbp_header_t) +
203            sizeof (udp_header_t) + sizeof (ip4_header_t));
204
205   ip0 = (ip4_header_t *) buff;
206
207   *outer_src = ip0->src_address;
208   *outer_dst = ip0->dst_address;
209 }
210
211 static uword
212 gbp_learn_l2 (vlib_main_t * vm,
213               vlib_node_runtime_t * node, vlib_frame_t * frame)
214 {
215   u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
216   gbp_learn_main_t *glm;
217   f64 time_now;
218
219   glm = &gbp_learn_main;
220   next_index = 0;
221   n_left_from = frame->n_vectors;
222   from = vlib_frame_vector_args (frame);
223   time_now = vlib_time_now (vm);
224   thread_index = vm->thread_index;
225
226   seed = throttle_seed (&glm->gl_l2_throttle, thread_index, time_now);
227
228   while (n_left_from > 0)
229     {
230       u32 n_left_to_next;
231
232       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
233
234       while (n_left_from > 0 && n_left_to_next > 0)
235         {
236           ip4_address_t outer_src, outer_dst;
237           u32 bi0, sw_if_index0, t0, epg0;
238           const ethernet_header_t *eh0;
239           gbp_bridge_domain_t *gb0;
240           gbp_learn_next_t next0;
241           gbp_endpoint_t *ge0;
242           vlib_buffer_t *b0;
243
244           next0 = GBP_LEARN_NEXT_DROP;
245           bi0 = from[0];
246           to_next[0] = bi0;
247           from += 1;
248           to_next += 1;
249           n_left_from -= 1;
250           n_left_to_next -= 1;
251
252           b0 = vlib_get_buffer (vm, bi0);
253           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
254
255           eh0 = vlib_buffer_get_current (b0);
256           epg0 = vnet_buffer2 (b0)->gbp.src_epg;
257
258           next0 = vnet_l2_feature_next (b0, glm->gl_l2_input_feat_next,
259                                         L2INPUT_FEAT_GBP_LEARN);
260
261           ge0 = gbp_endpoint_find_mac (eh0->src_address,
262                                        vnet_buffer (b0)->l2.bd_index);
263           gb0 =
264             gbp_bridge_domain_get_by_bd_index (vnet_buffer (b0)->l2.bd_index);
265
266           if ((vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D) ||
267               (gb0->gb_flags & GBP_BD_FLAG_DO_NOT_LEARN))
268             {
269               t0 = 1;
270               goto trace;
271             }
272
273           /*
274            * check for new EP or a moved EP
275            */
276           if (NULL == ge0 || ge0->ge_sw_if_index != sw_if_index0)
277
278             {
279               /*
280                * use the last 4 bytes of the mac address as the hash for the EP
281                */
282               t0 = throttle_check (&glm->gl_l2_throttle, thread_index,
283                                    *((u32 *) (eh0->src_address + 2)), seed);
284               if (!t0)
285                 {
286                   gbp_learn_get_outer (eh0, &outer_src, &outer_dst);
287
288                   switch (clib_net_to_host_u16 (eh0->type))
289                     {
290                     case ETHERNET_TYPE_IP4:
291                       {
292                         const ip4_header_t *ip0;
293
294                         ip0 = (ip4_header_t *) (eh0 + 1);
295
296                         gbp_learn_l2_ip4_dp (eh0->src_address,
297                                              &ip0->src_address,
298                                              vnet_buffer (b0)->l2.bd_index,
299                                              sw_if_index0, epg0,
300                                              &outer_src, &outer_dst);
301
302                         break;
303                       }
304                     case ETHERNET_TYPE_IP6:
305                       {
306                         const ip6_header_t *ip0;
307
308                         ip0 = (ip6_header_t *) (eh0 + 1);
309
310                         gbp_learn_l2_ip6_dp (eh0->src_address,
311                                              &ip0->src_address,
312                                              vnet_buffer (b0)->l2.bd_index,
313                                              sw_if_index0, epg0,
314                                              &outer_src, &outer_dst);
315
316                         break;
317                       }
318                     default:
319                       gbp_learn_l2_dp (eh0->src_address,
320                                        vnet_buffer (b0)->l2.bd_index,
321                                        sw_if_index0, epg0,
322                                        &outer_src, &outer_dst);
323                       break;
324                     }
325                 }
326             }
327           else
328             {
329               /*
330                * this update could happen simultaneoulsy from multiple workers
331                * but that's ok we are not interested in being very accurate.
332                */
333               t0 = 0;
334               ge0->ge_last_time = time_now;
335             }
336         trace:
337           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
338             {
339               gbp_learn_l2_trace_t *t =
340                 vlib_add_trace (vm, node, b0, sizeof (*t));
341               clib_memcpy (t->mac.bytes, eh0->src_address, 6);
342               t->new = (NULL == ge0);
343               t->throttled = t0;
344               t->sw_if_index = sw_if_index0;
345               t->epg = epg0;
346               t->d_bit = ! !(vnet_buffer2 (b0)->gbp.flags &
347                              VXLAN_GBP_GPFLAGS_D);
348             }
349
350           /* verify speculative enqueue, maybe switch current next frame */
351           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
352                                            to_next, n_left_to_next,
353                                            bi0, next0);
354         }
355
356       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
357     }
358
359   return frame->n_vectors;
360 }
361
362 /* packet trace format function */
363 static u8 *
364 format_gbp_learn_l2_trace (u8 * s, va_list * args)
365 {
366   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
367   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
368   gbp_learn_l2_trace_t *t = va_arg (*args, gbp_learn_l2_trace_t *);
369
370   s = format (s, "new:%d throttled:%d d-bit:%d mac:%U itf:%d epg:%d",
371               t->new, t->throttled, t->d_bit,
372               format_mac_address_t, &t->mac, t->sw_if_index, t->epg);
373
374   return s;
375 }
376
377 /* *INDENT-OFF* */
378 VLIB_REGISTER_NODE (gbp_learn_l2_node) = {
379   .function = gbp_learn_l2,
380   .name = "gbp-learn-l2",
381   .vector_size = sizeof (u32),
382   .format_trace = format_gbp_learn_l2_trace,
383   .type = VLIB_NODE_TYPE_INTERNAL,
384
385   .n_errors = ARRAY_LEN(gbp_learn_error_strings),
386   .error_strings = gbp_learn_error_strings,
387
388   .n_next_nodes = GBP_LEARN_N_NEXT,
389
390   .next_nodes = {
391     [GBP_LEARN_NEXT_DROP] = "error-drop",
392   },
393 };
394
395 VLIB_NODE_FUNCTION_MULTIARCH (gbp_learn_l2_node, gbp_learn_l2);
396 /* *INDENT-ON* */
397
398 typedef struct gbp_learn_l3_t_
399 {
400   ip46_address_t ip;
401   u32 fib_index;
402   u32 sw_if_index;
403   epg_id_t epg;
404   ip46_address_t outer_src;
405   ip46_address_t outer_dst;
406 } gbp_learn_l3_t;
407
408 static void
409 gbp_learn_l3_cp (const gbp_learn_l3_t * gl3)
410 {
411   ip46_address_t *ips = NULL;
412
413   GBP_LEARN_DBG ("L3 EP: %U, %d", format_ip46_address, &gl3->ip,
414                  IP46_TYPE_ANY, gl3->epg);
415
416   vec_add1 (ips, gl3->ip);
417
418   gbp_endpoint_update (gl3->sw_if_index, ips, NULL, gl3->epg,
419                        (GBP_ENDPOINT_FLAG_REMOTE |
420                         GBP_ENDPOINT_FLAG_LEARNT),
421                        &gl3->outer_dst, &gl3->outer_src, NULL);
422 }
423
424 static void
425 gbp_learn_ip4_dp (const ip4_address_t * ip,
426                   u32 fib_index, u32 sw_if_index, epg_id_t epg,
427                   const ip4_address_t * outer_src,
428                   const ip4_address_t * outer_dst)
429 {
430   /* *INDENT-OFF* */
431   gbp_learn_l3_t gl3 = {
432     .ip = {
433       .ip4 = *ip,
434     },
435     .sw_if_index = sw_if_index,
436     .fib_index = fib_index,
437     .epg = epg,
438     .outer_src.ip4 = *outer_src,
439     .outer_dst.ip4 = *outer_dst,
440   };
441   /* *INDENT-ON* */
442
443   vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
444 }
445
446 static void
447 gbp_learn_ip6_dp (const ip6_address_t * ip,
448                   u32 fib_index, u32 sw_if_index, epg_id_t epg,
449                   const ip4_address_t * outer_src,
450                   const ip4_address_t * outer_dst)
451 {
452   /* *INDENT-OFF* */
453   gbp_learn_l3_t gl3 = {
454     .ip = {
455       .ip6 = *ip,
456     },
457     .sw_if_index = sw_if_index,
458     .fib_index = fib_index,
459     .epg = epg,
460     .outer_src.ip4 = *outer_src,
461     .outer_dst.ip4 = *outer_dst,
462   };
463   /* *INDENT-ON* */
464
465   vl_api_rpc_call_main_thread (gbp_learn_l3_cp, (u8 *) & gl3, sizeof (gl3));
466 }
467
468 /**
469  * per-packet trace data
470  */
471 typedef struct gbp_learn_l3_trace_t_
472 {
473   /* per-pkt trace data */
474   ip46_address_t ip;
475   u32 sw_if_index;
476   u32 new;
477   u32 throttled;
478   u32 epg;
479 } gbp_learn_l3_trace_t;
480
481 static uword
482 gbp_learn_l3 (vlib_main_t * vm,
483               vlib_node_runtime_t * node, vlib_frame_t * frame,
484               fib_protocol_t fproto)
485 {
486   u32 n_left_from, *from, *to_next, next_index, thread_index, seed;
487   gbp_learn_main_t *glm;
488   f64 time_now;
489
490   glm = &gbp_learn_main;
491   next_index = 0;
492   n_left_from = frame->n_vectors;
493   from = vlib_frame_vector_args (frame);
494   time_now = vlib_time_now (vm);
495   thread_index = vm->thread_index;
496
497   seed = throttle_seed (&glm->gl_l3_throttle, thread_index, time_now);
498
499   while (n_left_from > 0)
500     {
501       u32 n_left_to_next;
502
503       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
504
505       while (n_left_from > 0 && n_left_to_next > 0)
506         {
507           u32 bi0, sw_if_index0, t0, epg0, fib_index0;
508           CLIB_UNUSED (const ip4_header_t *) ip4_0;
509           CLIB_UNUSED (const ip6_header_t *) ip6_0;
510           ip4_address_t outer_src, outer_dst;
511           ethernet_header_t *eth0;
512           gbp_learn_next_t next0;
513           gbp_endpoint_t *ge0;
514           vlib_buffer_t *b0;
515
516           next0 = GBP_LEARN_NEXT_DROP;
517           bi0 = from[0];
518           to_next[0] = bi0;
519           from += 1;
520           to_next += 1;
521           n_left_from -= 1;
522           n_left_to_next -= 1;
523
524           b0 = vlib_get_buffer (vm, bi0);
525           sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
526           epg0 = vnet_buffer2 (b0)->gbp.src_epg;
527           ip6_0 = NULL;
528           ip4_0 = NULL;
529
530           vnet_feature_next (&next0, b0);
531
532           if (vnet_buffer2 (b0)->gbp.flags & VXLAN_GBP_GPFLAGS_D)
533             {
534               t0 = 1;
535               ge0 = NULL;
536               goto trace;
537             }
538
539           fib_index0 = fib_table_get_index_for_sw_if_index (fproto,
540                                                             sw_if_index0);
541
542           if (FIB_PROTOCOL_IP6 == fproto)
543             {
544               ip6_0 = vlib_buffer_get_current (b0);
545               eth0 = (ethernet_header_t *) (((u8 *) ip6_0) - sizeof (*eth0));
546
547               gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
548
549               ge0 = gbp_endpoint_find_ip6 (&ip6_0->src_address, fib_index0);
550
551               if (NULL == ge0)
552                 {
553                   t0 = throttle_check (&glm->gl_l3_throttle,
554                                        thread_index,
555                                        ip6_address_hash_to_u32
556                                        (&ip6_0->src_address), seed);
557
558                   if (!t0)
559                     {
560                       gbp_learn_ip6_dp (&ip6_0->src_address,
561                                         fib_index0, sw_if_index0, epg0,
562                                         &outer_src, &outer_dst);
563                     }
564                 }
565               else
566                 {
567                   /*
568                    * this update could happen simultaneoulsy from multiple
569                    * workers but that's ok we are not interested in being
570                    * very accurate.
571                    */
572                   t0 = 0;
573                   ge0->ge_last_time = time_now;
574                 }
575             }
576           else
577             {
578               ip4_0 = vlib_buffer_get_current (b0);
579               eth0 = (ethernet_header_t *) (((u8 *) ip4_0) - sizeof (*eth0));
580
581               gbp_learn_get_outer (eth0, &outer_src, &outer_dst);
582               ge0 = gbp_endpoint_find_ip4 (&ip4_0->src_address, fib_index0);
583
584               if (NULL == ge0)
585                 {
586                   t0 = throttle_check (&glm->gl_l3_throttle, thread_index,
587                                        ip4_0->src_address.as_u32, seed);
588
589                   if (!t0)
590                     {
591                       gbp_learn_ip4_dp (&ip4_0->src_address,
592                                         fib_index0, sw_if_index0, epg0,
593                                         &outer_src, &outer_dst);
594                     }
595                 }
596               else
597                 {
598                   /*
599                    * this update could happen simultaneoulsy from multiple
600                    * workers but that's ok we are not interested in being
601                    * very accurate.
602                    */
603                   t0 = 0;
604                   ge0->ge_last_time = time_now;
605                 }
606             }
607         trace:
608           if (PREDICT_FALSE ((b0->flags & VLIB_BUFFER_IS_TRACED)))
609             {
610               gbp_learn_l3_trace_t *t;
611
612               t = vlib_add_trace (vm, node, b0, sizeof (*t));
613               if (FIB_PROTOCOL_IP6 == fproto && ip6_0)
614                 ip46_address_set_ip6 (&t->ip, &ip6_0->src_address);
615               if (FIB_PROTOCOL_IP4 == fproto && ip4_0)
616                 ip46_address_set_ip4 (&t->ip, &ip4_0->src_address);
617               t->new = (NULL == ge0);
618               t->throttled = t0;
619               t->sw_if_index = sw_if_index0;
620               t->epg = epg0;
621             }
622
623           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
624                                            to_next, n_left_to_next,
625                                            bi0, next0);
626         }
627
628       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
629     }
630
631   return frame->n_vectors;
632 }
633
634 /* packet trace format function */
635 static u8 *
636 format_gbp_learn_l3_trace (u8 * s, va_list * args)
637 {
638   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
639   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
640   gbp_learn_l3_trace_t *t = va_arg (*args, gbp_learn_l3_trace_t *);
641
642   s = format (s, "new:%d throttled:%d ip:%U itf:%d epg:%d",
643               t->new, t->throttled,
644               format_ip46_address, &t->ip, IP46_TYPE_ANY, t->sw_if_index,
645               t->epg);
646
647   return s;
648 }
649
650 static uword
651 gbp_learn_ip4 (vlib_main_t * vm,
652                vlib_node_runtime_t * node, vlib_frame_t * frame)
653 {
654   return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP4));
655 }
656
657 static uword
658 gbp_learn_ip6 (vlib_main_t * vm,
659                vlib_node_runtime_t * node, vlib_frame_t * frame)
660 {
661   return (gbp_learn_l3 (vm, node, frame, FIB_PROTOCOL_IP6));
662 }
663
664 /* *INDENT-OFF* */
665 VLIB_REGISTER_NODE (gbp_learn_ip4_node) = {
666   .function = gbp_learn_ip4,
667   .name = "gbp-learn-ip4",
668   .vector_size = sizeof (u32),
669   .format_trace = format_gbp_learn_l3_trace,
670   .type = VLIB_NODE_TYPE_INTERNAL,
671 };
672
673 VLIB_NODE_FUNCTION_MULTIARCH (gbp_learn_ip4_node, gbp_learn_ip4);
674
675 VNET_FEATURE_INIT (gbp_learn_ip4, static) =
676 {
677   .arc_name = "ip4-unicast",
678   .node_name = "gbp-learn-ip4",
679 };
680
681 VLIB_REGISTER_NODE (gbp_learn_ip6_node) = {
682   .function = gbp_learn_ip6,
683   .name = "gbp-learn-ip6",
684   .vector_size = sizeof (u32),
685   .format_trace = format_gbp_learn_l3_trace,
686   .type = VLIB_NODE_TYPE_INTERNAL,
687 };
688
689 VLIB_NODE_FUNCTION_MULTIARCH (gbp_learn_ip6_node, gbp_learn_ip6);
690
691 VNET_FEATURE_INIT (gbp_learn_ip6, static) =
692 {
693   .arc_name = "ip6-unicast",
694   .node_name = "gbp-learn-ip6",
695 };
696
697 /* *INDENT-ON* */
698
699 void
700 gbp_learn_enable (u32 sw_if_index, gbb_learn_mode_t mode)
701 {
702   if (GBP_LEARN_MODE_L2 == mode)
703     l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_GBP_LEARN, 1);
704   else
705     {
706       vnet_feature_enable_disable ("ip4-unicast",
707                                    "gbp-learn-ip4", sw_if_index, 1, 0, 0);
708       vnet_feature_enable_disable ("ip6-unicast",
709                                    "gbp-learn-ip6", sw_if_index, 1, 0, 0);
710     }
711 }
712
713 void
714 gbp_learn_disable (u32 sw_if_index, gbb_learn_mode_t mode)
715 {
716   if (GBP_LEARN_MODE_L2 == mode)
717     l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_GBP_LEARN, 0);
718   else
719     {
720       vnet_feature_enable_disable ("ip4-unicast",
721                                    "gbp-learn-ip4", sw_if_index, 0, 0, 0);
722       vnet_feature_enable_disable ("ip6-unicast",
723                                    "gbp-learn-ip6", sw_if_index, 0, 0, 0);
724     }
725 }
726
727 static clib_error_t *
728 gbp_learn_init (vlib_main_t * vm)
729 {
730   gbp_learn_main_t *glm = &gbp_learn_main;
731   vlib_thread_main_t *tm = &vlib_thread_main;
732
733   /* Initialize the feature next-node indices */
734   feat_bitmap_init_next_nodes (vm,
735                                gbp_learn_l2_node.index,
736                                L2INPUT_N_FEAT,
737                                l2input_get_feat_names (),
738                                glm->gl_l2_input_feat_next);
739
740   throttle_init (&glm->gl_l2_throttle,
741                  tm->n_vlib_mains, GBP_ENDPOINT_HASH_LEARN_RATE);
742
743   throttle_init (&glm->gl_l3_throttle,
744                  tm->n_vlib_mains, GBP_ENDPOINT_HASH_LEARN_RATE);
745
746   glm->gl_logger = vlib_log_register_class ("gbp", "learn");
747
748   return 0;
749 }
750
751 VLIB_INIT_FUNCTION (gbp_learn_init);
752
753 /*
754  * fd.io coding-style-patch-verification: ON
755  *
756  * Local Variables:
757  * eval: (c-set-style "gnu")
758  * End:
759  */