fib: Decouple source from priority and behaviour
[vpp.git] / src / plugins / pppoe / pppoe.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Intel and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 #include <stdint.h>
18 #include <net/if.h>
19 #include <sys/ioctl.h>
20 #include <inttypes.h>
21
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/ethernet/ethernet.h>
25 #include <vnet/fib/fib_entry.h>
26 #include <vnet/fib/fib_table.h>
27 #include <vnet/dpo/interface_tx_dpo.h>
28 #include <vnet/plugin/plugin.h>
29 #include <vpp/app/version.h>
30 #include <vnet/ppp/packet.h>
31 #include <pppoe/pppoe.h>
32 #include <vnet/adj/adj_midchain.h>
33 #include <vnet/adj/adj_mcast.h>
34
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bihash_template.c>
37
38 pppoe_main_t pppoe_main;
39
40 static fib_source_t pppoe_fib_src;
41
42 u8 *
43 format_pppoe_session (u8 * s, va_list * args)
44 {
45   pppoe_session_t *t = va_arg (*args, pppoe_session_t *);
46   pppoe_main_t *pem = &pppoe_main;
47
48   s = format (s, "[%d] sw-if-index %d client-ip %U session-id %d ",
49               t - pem->sessions, t->sw_if_index,
50               format_ip46_address, &t->client_ip, IP46_TYPE_ANY,
51               t->session_id);
52
53   s = format (s, "encap-if-index %d decap-fib-index %d\n",
54               t->encap_if_index, t->decap_fib_index);
55
56   s = format (s, "    local-mac %U  client-mac %U",
57               format_ethernet_address, t->local_mac,
58               format_ethernet_address, t->client_mac);
59
60   return s;
61 }
62
63 static u8 *
64 format_pppoe_name (u8 * s, va_list * args)
65 {
66   u32 dev_instance = va_arg (*args, u32);
67   return format (s, "pppoe_session%d", dev_instance);
68 }
69
70 static clib_error_t *
71 pppoe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
72 {
73   u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ?
74     VNET_HW_INTERFACE_FLAG_LINK_UP : 0;
75   vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags);
76
77   return /* no error */ 0;
78 }
79
80 /* *INDENT-OFF* */
81 VNET_DEVICE_CLASS (pppoe_device_class,static) = {
82   .name = "PPPoE",
83   .format_device_name = format_pppoe_name,
84   .admin_up_down_function = pppoe_interface_admin_up_down,
85 };
86 /* *INDENT-ON* */
87
88 static u8 *
89 format_pppoe_header_with_length (u8 * s, va_list * args)
90 {
91   u32 dev_instance = va_arg (*args, u32);
92   s = format (s, "unimplemented dev %u", dev_instance);
93   return s;
94 }
95
96 static u8 *
97 pppoe_build_rewrite (vnet_main_t * vnm,
98                      u32 sw_if_index,
99                      vnet_link_t link_type, const void *dst_address)
100 {
101   int len = sizeof (pppoe_header_t) + sizeof (ethernet_header_t);
102   pppoe_main_t *pem = &pppoe_main;
103   pppoe_session_t *t;
104   u32 session_id;
105   u8 *rw = 0;
106
107   session_id = pem->session_index_by_sw_if_index[sw_if_index];
108   t = pool_elt_at_index (pem->sessions, session_id);
109
110   vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
111
112   ethernet_header_t *eth_hdr = (ethernet_header_t *) rw;
113   clib_memcpy (eth_hdr->dst_address, t->client_mac, 6);
114   clib_memcpy (eth_hdr->src_address, t->local_mac, 6);
115   eth_hdr->type = clib_host_to_net_u16 (ETHERNET_TYPE_PPPOE_SESSION);
116
117   pppoe_header_t *pppoe = (pppoe_header_t *) (eth_hdr + 1);
118   pppoe->ver_type = PPPOE_VER_TYPE;
119   pppoe->code = 0;
120   pppoe->session_id = clib_host_to_net_u16 (t->session_id);
121   pppoe->length = 0;            /* To be filled in at run-time */
122
123   switch (link_type)
124     {
125     case VNET_LINK_IP4:
126       pppoe->ppp_proto = clib_host_to_net_u16 (PPP_PROTOCOL_ip4);
127       break;
128     case VNET_LINK_IP6:
129       pppoe->ppp_proto = clib_host_to_net_u16 (PPP_PROTOCOL_ip6);
130       break;
131     default:
132       break;
133     }
134
135   return rw;
136 }
137
138 /**
139  * @brief Fixup the adj rewrite post encap. Insert the packet's length
140  */
141 static void
142 pppoe_fixup (vlib_main_t * vm,
143              const ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data)
144 {
145   const pppoe_session_t *t;
146   pppoe_header_t *pppoe0;
147
148   /* update the rewrite string */
149   pppoe0 = vlib_buffer_get_current (b0) + sizeof (ethernet_header_t);
150
151   pppoe0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
152                                          - sizeof (pppoe_header_t)
153                                          + sizeof (pppoe0->ppp_proto)
154                                          - sizeof (ethernet_header_t));
155   /* Swap to the the packet's output interface to the encap (not the
156    * session) interface */
157   t = data;
158   vnet_buffer (b0)->sw_if_index[VLIB_TX] = t->encap_if_index;
159 }
160
161 static void
162 pppoe_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
163 {
164   pppoe_main_t *pem = &pppoe_main;
165   dpo_id_t dpo = DPO_INVALID;
166   ip_adjacency_t *adj;
167   pppoe_session_t *t;
168   u32 session_id;
169
170   ASSERT (ADJ_INDEX_INVALID != ai);
171
172   adj = adj_get (ai);
173   session_id = pem->session_index_by_sw_if_index[sw_if_index];
174   t = pool_elt_at_index (pem->sessions, session_id);
175
176   switch (adj->lookup_next_index)
177     {
178     case IP_LOOKUP_NEXT_ARP:
179     case IP_LOOKUP_NEXT_GLEAN:
180     case IP_LOOKUP_NEXT_BCAST:
181       adj_nbr_midchain_update_rewrite (ai, pppoe_fixup, t,
182                                        ADJ_FLAG_NONE,
183                                        pppoe_build_rewrite (vnm,
184                                                             sw_if_index,
185                                                             adj->ia_link,
186                                                             NULL));
187       break;
188     case IP_LOOKUP_NEXT_MCAST:
189       /*
190        * Construct a partial rewrite from the known ethernet mcast dest MAC
191        * There's no MAC fixup, so the last 2 parameters are 0
192        */
193       adj_mcast_midchain_update_rewrite (ai, pppoe_fixup, t,
194                                          ADJ_FLAG_NONE,
195                                          pppoe_build_rewrite (vnm,
196                                                               sw_if_index,
197                                                               adj->ia_link,
198                                                               NULL), 0, 0);
199       break;
200
201     case IP_LOOKUP_NEXT_DROP:
202     case IP_LOOKUP_NEXT_PUNT:
203     case IP_LOOKUP_NEXT_LOCAL:
204     case IP_LOOKUP_NEXT_REWRITE:
205     case IP_LOOKUP_NEXT_MIDCHAIN:
206     case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
207     case IP_LOOKUP_NEXT_ICMP_ERROR:
208     case IP_LOOKUP_N_NEXT:
209       ASSERT (0);
210       break;
211     }
212
213   interface_tx_dpo_add_or_lock (vnet_link_to_dpo_proto (adj->ia_link),
214                                 t->encap_if_index, &dpo);
215
216   adj_nbr_midchain_stack (ai, &dpo);
217
218   dpo_reset (&dpo);
219 }
220
221 /* *INDENT-OFF* */
222 VNET_HW_INTERFACE_CLASS (pppoe_hw_class) =
223 {
224   .name = "PPPoE",
225   .format_header = format_pppoe_header_with_length,
226   .build_rewrite = pppoe_build_rewrite,
227   .update_adjacency = pppoe_update_adj,
228   .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
229 };
230 /* *INDENT-ON* */
231
232 #define foreach_copy_field                      \
233 _(session_id)                                   \
234 _(encap_if_index)                               \
235 _(decap_fib_index)                              \
236 _(client_ip)
237
238 static bool
239 pppoe_decap_next_is_valid (pppoe_main_t * pem, u32 is_ip6,
240                            u32 decap_fib_index)
241 {
242   vlib_main_t *vm = pem->vlib_main;
243   u32 input_idx = (!is_ip6) ? ip4_input_node.index : ip6_input_node.index;
244   vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
245
246   return decap_fib_index < r->n_next_nodes;
247 }
248
249 int vnet_pppoe_add_del_session
250   (vnet_pppoe_add_del_session_args_t * a, u32 * sw_if_indexp)
251 {
252   pppoe_main_t *pem = &pppoe_main;
253   pppoe_session_t *t = 0;
254   vnet_main_t *vnm = pem->vnet_main;
255   u32 hw_if_index = ~0;
256   u32 sw_if_index = ~0;
257   u32 is_ip6 = a->is_ip6;
258   pppoe_entry_key_t cached_key;
259   pppoe_entry_result_t cached_result;
260   u32 bucket;
261   pppoe_entry_key_t key;
262   pppoe_entry_result_t result;
263   vnet_hw_interface_t *hi;
264   vnet_sw_interface_t *si;
265   fib_prefix_t pfx;
266
267   cached_key.raw = ~0;
268   cached_result.raw = ~0;       /* warning be gone */
269   clib_memset (&pfx, 0, sizeof (pfx));
270
271   if (!is_ip6)
272     {
273       pfx.fp_addr.ip4.as_u32 = a->client_ip.ip4.as_u32;
274       pfx.fp_len = 32;
275       pfx.fp_proto = FIB_PROTOCOL_IP4;
276     }
277   else
278     {
279       pfx.fp_addr.ip6.as_u64[0] = a->client_ip.ip6.as_u64[0];
280       pfx.fp_addr.ip6.as_u64[1] = a->client_ip.ip6.as_u64[1];
281       pfx.fp_len = 128;
282       pfx.fp_proto = FIB_PROTOCOL_IP6;
283     }
284
285   /* Get encap_if_index and local mac address from link_table */
286   pppoe_lookup_1 (&pem->link_table, &cached_key, &cached_result,
287                   a->client_mac, 0, &key, &bucket, &result);
288   a->encap_if_index = result.fields.sw_if_index;
289
290   if (a->encap_if_index == ~0)
291     return VNET_API_ERROR_INVALID_SW_IF_INDEX;
292
293   si = vnet_get_sw_interface (vnm, a->encap_if_index);
294   hi = vnet_get_hw_interface (vnm, si->hw_if_index);
295
296   /* lookup session_table */
297   pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
298                   a->client_mac, clib_host_to_net_u16 (a->session_id),
299                   &key, &bucket, &result);
300
301   /* learn client session */
302   pppoe_learn_process (&pem->session_table, a->encap_if_index,
303                        &key, &cached_key, &bucket, &result);
304
305   if (a->is_add)
306     {
307       /* adding a session: session must not already exist */
308       if (result.fields.session_index != ~0)
309         return VNET_API_ERROR_TUNNEL_EXIST;
310
311       /*if not set explicitly, default to ip4 */
312       if (!pppoe_decap_next_is_valid (pem, is_ip6, a->decap_fib_index))
313         return VNET_API_ERROR_INVALID_DECAP_NEXT;
314
315       pool_get_aligned (pem->sessions, t, CLIB_CACHE_LINE_BYTES);
316       clib_memset (t, 0, sizeof (*t));
317
318       clib_memcpy (t->local_mac, hi->hw_address, 6);
319
320       /* copy from arg structure */
321 #define _(x) t->x = a->x;
322       foreach_copy_field;
323 #undef _
324
325       clib_memcpy (t->client_mac, a->client_mac, 6);
326
327       /* update pppoe fib with session_index */
328       result.fields.session_index = t - pem->sessions;
329       pppoe_update_1 (&pem->session_table,
330                       a->client_mac, clib_host_to_net_u16 (a->session_id),
331                       &key, &bucket, &result);
332
333       vnet_hw_interface_t *hi;
334       if (vec_len (pem->free_pppoe_session_hw_if_indices) > 0)
335         {
336           vnet_interface_main_t *im = &vnm->interface_main;
337           hw_if_index = pem->free_pppoe_session_hw_if_indices
338             [vec_len (pem->free_pppoe_session_hw_if_indices) - 1];
339           _vec_len (pem->free_pppoe_session_hw_if_indices) -= 1;
340
341           hi = vnet_get_hw_interface (vnm, hw_if_index);
342           hi->dev_instance = t - pem->sessions;
343           hi->hw_instance = hi->dev_instance;
344
345           /* clear old stats of freed session before reuse */
346           sw_if_index = hi->sw_if_index;
347           vnet_interface_counter_lock (im);
348           vlib_zero_combined_counter
349             (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
350              sw_if_index);
351           vlib_zero_combined_counter (&im->combined_sw_if_counters
352                                       [VNET_INTERFACE_COUNTER_RX],
353                                       sw_if_index);
354           vlib_zero_simple_counter (&im->sw_if_counters
355                                     [VNET_INTERFACE_COUNTER_DROP],
356                                     sw_if_index);
357           vnet_interface_counter_unlock (im);
358         }
359       else
360         {
361           hw_if_index = vnet_register_interface
362             (vnm, pppoe_device_class.index, t - pem->sessions,
363              pppoe_hw_class.index, t - pem->sessions);
364           hi = vnet_get_hw_interface (vnm, hw_if_index);
365         }
366
367       t->hw_if_index = hw_if_index;
368       t->sw_if_index = sw_if_index = hi->sw_if_index;
369
370       vec_validate_init_empty (pem->session_index_by_sw_if_index, sw_if_index,
371                                ~0);
372       pem->session_index_by_sw_if_index[sw_if_index] = t - pem->sessions;
373
374       vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
375       si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
376       vnet_sw_interface_set_flags (vnm, sw_if_index,
377                                    VNET_SW_INTERFACE_FLAG_ADMIN_UP);
378
379       /* add reverse route for client ip */
380       fib_table_entry_path_add (a->decap_fib_index, &pfx,
381                                 pppoe_fib_src, FIB_ENTRY_FLAG_NONE,
382                                 fib_proto_to_dpo (pfx.fp_proto),
383                                 &pfx.fp_addr, sw_if_index, ~0,
384                                 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
385
386     }
387   else
388     {
389       /* deleting a session: session must exist */
390       if (result.fields.session_index == ~0)
391         return VNET_API_ERROR_NO_SUCH_ENTRY;
392
393       t = pool_elt_at_index (pem->sessions, result.fields.session_index);
394       sw_if_index = t->sw_if_index;
395
396       vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
397       vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
398       si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
399
400       vec_add1 (pem->free_pppoe_session_hw_if_indices, t->hw_if_index);
401
402       pem->session_index_by_sw_if_index[t->sw_if_index] = ~0;
403
404       /* update pppoe fib with session_inde=~0x */
405       result.fields.session_index = ~0;
406       pppoe_update_1 (&pem->session_table,
407                       a->client_mac, clib_host_to_net_u16 (a->session_id),
408                       &key, &bucket, &result);
409
410
411       /* delete reverse route for client ip */
412       fib_table_entry_path_remove (a->decap_fib_index, &pfx,
413                                    pppoe_fib_src,
414                                    fib_proto_to_dpo (pfx.fp_proto),
415                                    &pfx.fp_addr,
416                                    sw_if_index, ~0, 1,
417                                    FIB_ROUTE_PATH_FLAG_NONE);
418
419       pool_put (pem->sessions, t);
420     }
421
422   if (sw_if_indexp)
423     *sw_if_indexp = sw_if_index;
424
425   return 0;
426 }
427
428 static clib_error_t *
429 pppoe_add_del_session_command_fn (vlib_main_t * vm,
430                                   unformat_input_t * input,
431                                   vlib_cli_command_t * cmd)
432 {
433   unformat_input_t _line_input, *line_input = &_line_input;
434   u16 session_id = 0;
435   ip46_address_t client_ip;
436   u8 is_add = 1;
437   u8 client_ip_set = 0;
438   u8 ipv4_set = 0;
439   u8 ipv6_set = 0;
440   u32 encap_if_index = 0;
441   u32 decap_fib_index = 0;
442   u8 client_mac[6] = { 0 };
443   u8 client_mac_set = 0;
444   int rv;
445   u32 tmp;
446   vnet_pppoe_add_del_session_args_t _a, *a = &_a;
447   u32 session_sw_if_index;
448   clib_error_t *error = NULL;
449
450   /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
451   clib_memset (&client_ip, 0, sizeof client_ip);
452
453   /* Get a line of input. */
454   if (!unformat_user (input, unformat_line_input, line_input))
455     return 0;
456
457   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
458     {
459       if (unformat (line_input, "del"))
460         {
461           is_add = 0;
462         }
463       else if (unformat (line_input, "session-id %d", &session_id))
464         ;
465       else if (unformat (line_input, "client-ip %U",
466                          unformat_ip4_address, &client_ip.ip4))
467         {
468           client_ip_set = 1;
469           ipv4_set = 1;
470         }
471       else if (unformat (line_input, "client-ip %U",
472                          unformat_ip6_address, &client_ip.ip6))
473         {
474           client_ip_set = 1;
475           ipv6_set = 1;
476         }
477       else if (unformat (line_input, "decap-vrf-id %d", &tmp))
478         {
479           if (ipv6_set)
480             decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
481           else
482             decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
483
484           if (decap_fib_index == ~0)
485             {
486               error =
487                 clib_error_return (0, "nonexistent decap fib id %d", tmp);
488               goto done;
489             }
490         }
491       else
492         if (unformat
493             (line_input, "client-mac %U", unformat_ethernet_address,
494              client_mac))
495         client_mac_set = 1;
496       else
497         {
498           error = clib_error_return (0, "parse error: '%U'",
499                                      format_unformat_error, line_input);
500           goto done;
501         }
502     }
503
504   if (client_ip_set == 0)
505     {
506       error =
507         clib_error_return (0, "session client ip address not specified");
508       goto done;
509     }
510
511   if (ipv4_set && ipv6_set)
512     {
513       error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
514       goto done;
515     }
516
517   if (client_mac_set == 0)
518     {
519       error = clib_error_return (0, "session client mac not specified");
520       goto done;
521     }
522
523   clib_memset (a, 0, sizeof (*a));
524
525   a->is_add = is_add;
526   a->is_ip6 = ipv6_set;
527
528 #define _(x) a->x = x;
529   foreach_copy_field;
530 #undef _
531
532   clib_memcpy (a->client_mac, client_mac, 6);
533
534   rv = vnet_pppoe_add_del_session (a, &session_sw_if_index);
535
536   switch (rv)
537     {
538     case 0:
539       if (is_add)
540         vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
541                          vnet_get_main (), session_sw_if_index);
542       break;
543
544     case VNET_API_ERROR_TUNNEL_EXIST:
545       error = clib_error_return (0, "session already exists...");
546       goto done;
547
548     case VNET_API_ERROR_NO_SUCH_ENTRY:
549       error = clib_error_return (0, "session does not exist...");
550       goto done;
551
552     default:
553       error = clib_error_return
554         (0, "vnet_pppoe_add_del_session returned %d", rv);
555       goto done;
556     }
557
558 done:
559   unformat_free (line_input);
560
561   return error;
562 }
563
564 /*?
565  * Add or delete a PPPoE Session.
566  *
567  * @cliexpar
568  * Example of how to create a PPPoE Session:
569  * @cliexcmd{create pppoe session client-ip 10.0.3.1 session-id 13
570  *             client-mac 00:01:02:03:04:05 }
571  * Example of how to delete a PPPoE Session:
572  * @cliexcmd{create pppoe session client-ip 10.0.3.1 session-id 13
573  *             client-mac 00:01:02:03:04:05 del }
574  ?*/
575 /* *INDENT-OFF* */
576 VLIB_CLI_COMMAND (create_pppoe_session_command, static) = {
577   .path = "create pppoe session",
578   .short_help =
579   "create pppoe session client-ip <client-ip> session-id <nn>"
580   " client-mac <client-mac> [decap-vrf-id <nn>] [del]",
581   .function = pppoe_add_del_session_command_fn,
582 };
583 /* *INDENT-ON* */
584
585 /* *INDENT-OFF* */
586 static clib_error_t *
587 show_pppoe_session_command_fn (vlib_main_t * vm,
588                                unformat_input_t * input,
589                                vlib_cli_command_t * cmd)
590 {
591   pppoe_main_t *pem = &pppoe_main;
592   pppoe_session_t *t;
593
594   if (pool_elts (pem->sessions) == 0)
595     vlib_cli_output (vm, "No pppoe sessions configured...");
596
597   pool_foreach (t, pem->sessions,
598                 ({
599                     vlib_cli_output (vm, "%U",format_pppoe_session, t);
600                 }));
601
602   return 0;
603 }
604 /* *INDENT-ON* */
605
606 /*?
607  * Display all the PPPoE Session entries.
608  *
609  * @cliexpar
610  * Example of how to display the PPPoE Session entries:
611  * @cliexstart{show pppoe session}
612  * [0] client-ip 10.0.3.1 session_id 13 encap-if-index 0 decap-vrf-id 13 sw_if_index 5
613  *     local-mac a0:b0:c0:d0:e0:f0 client-mac 00:01:02:03:04:05
614  * @cliexend
615  ?*/
616 /* *INDENT-OFF* */
617 VLIB_CLI_COMMAND (show_pppoe_session_command, static) = {
618     .path = "show pppoe session",
619     .short_help = "show pppoe session",
620     .function = show_pppoe_session_command_fn,
621 };
622 /* *INDENT-ON* */
623
624 typedef struct pppoe_show_walk_ctx_t_
625 {
626   vlib_main_t *vm;
627   u8 first_entry;
628   u32 total_entries;
629 } pppoe_show_walk_ctx_t;
630
631 static void
632 pppoe_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg)
633 {
634   pppoe_show_walk_ctx_t *ctx = arg;
635   pppoe_entry_result_t result;
636   pppoe_entry_key_t key;
637
638   if (ctx->first_entry)
639     {
640       ctx->first_entry = 0;
641       vlib_cli_output (ctx->vm,
642                        "%=19s%=12s%=13s%=14s",
643                        "Mac-Address", "session_id", "sw_if_index",
644                        "session_index");
645     }
646
647   key.raw = kvp->key;
648   result.raw = kvp->value;
649
650   vlib_cli_output (ctx->vm,
651                    "%=19U%=12d%=13d%=14d",
652                    format_ethernet_address, key.fields.mac,
653                    clib_net_to_host_u16 (key.fields.session_id),
654                    result.fields.sw_if_index == ~0
655                    ? -1 : result.fields.sw_if_index,
656                    result.fields.session_index == ~0
657                    ? -1 : result.fields.session_index);
658   ctx->total_entries++;
659 }
660
661 /** Display the contents of the PPPoE Fib. */
662 static clib_error_t *
663 show_pppoe_fib_command_fn (vlib_main_t * vm,
664                            unformat_input_t * input, vlib_cli_command_t * cmd)
665 {
666   pppoe_main_t *pem = &pppoe_main;
667   pppoe_show_walk_ctx_t ctx = {
668     .first_entry = 1,
669     .vm = vm,
670   };
671
672   BV (clib_bihash_foreach_key_value_pair)
673     (&pem->session_table, pppoe_show_walk_cb, &ctx);
674
675   if (ctx.total_entries == 0)
676     vlib_cli_output (vm, "no pppoe fib entries");
677   else
678     vlib_cli_output (vm, "%lld pppoe fib entries", ctx.total_entries);
679
680   return 0;
681 }
682
683 /*?
684  * This command dispays the MAC Address entries of the PPPoE FIB table.
685  * Output can be filtered to just get the number of MAC Addresses or display
686  * each MAC Address.
687  *
688  * @cliexpar
689  * Example of how to display the number of MAC Address entries in the PPPoE
690  * FIB table:
691  * @cliexstart{show pppoe fib}
692  *     Mac Address      session_id      Interface           sw_if_index  session_index
693  *  52:54:00:53:18:33     1          GigabitEthernet0/8/0        2          0
694  *  52:54:00:53:18:55     2          GigabitEthernet0/8/1        3          1
695  * @cliexend
696 ?*/
697 /* *INDENT-OFF* */
698 VLIB_CLI_COMMAND (show_pppoe_fib_command, static) = {
699     .path = "show pppoe fib",
700     .short_help = "show pppoe fib",
701     .function = show_pppoe_fib_command_fn,
702 };
703 /* *INDENT-ON* */
704
705 clib_error_t *
706 pppoe_init (vlib_main_t * vm)
707 {
708   pppoe_main_t *pem = &pppoe_main;
709
710   pem->vnet_main = vnet_get_main ();
711   pem->vlib_main = vm;
712
713   /* Create the hash table  */
714   BV (clib_bihash_init) (&pem->link_table, "pppoe link table",
715                          PPPOE_NUM_BUCKETS, PPPOE_MEMORY_SIZE);
716
717   BV (clib_bihash_init) (&pem->session_table, "pppoe session table",
718                          PPPOE_NUM_BUCKETS, PPPOE_MEMORY_SIZE);
719
720   ethernet_register_input_type (vm, ETHERNET_TYPE_PPPOE_SESSION,
721                                 pppoe_input_node.index);
722
723   ethernet_register_input_type (vm, ETHERNET_TYPE_PPPOE_DISCOVERY,
724                                 pppoe_cp_dispatch_node.index);
725
726   pppoe_fib_src = fib_source_allocate ("pppoe",
727                                        FIB_SOURCE_PRIORITY_HI,
728                                        FIB_SOURCE_BH_API);
729
730   return 0;
731 }
732
733 VLIB_INIT_FUNCTION (pppoe_init);
734
735 /* *INDENT-OFF* */
736 VLIB_PLUGIN_REGISTER () = {
737     .version = VPP_BUILD_VER,
738     .description = "PPP over Ethernet (PPPoE)",
739 };
740 /* *INDENT-ON* */
741
742 /*
743  * fd.io coding-style-patch-verification: ON
744  *
745  * Local Variables:
746  * eval: (c-set-style "gnu")
747  * End:
748  */