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