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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
19 #include <sys/ioctl.h>
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>
35 #include <vppinfra/hash.h>
36 #include <vppinfra/bihash_template.c>
38 pppoe_main_t pppoe_main;
41 format_pppoe_session (u8 * s, va_list * args)
43 pppoe_session_t *t = va_arg (*args, pppoe_session_t *);
44 pppoe_main_t *pem = &pppoe_main;
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,
51 s = format (s, "encap-if-index %d decap-fib-index %d\n",
52 t->encap_if_index, t->decap_fib_index);
54 s = format (s, " local-mac %U client-mac %U",
55 format_ethernet_address, t->local_mac,
56 format_ethernet_address, t->client_mac);
62 format_pppoe_name (u8 * s, va_list * args)
64 u32 dev_instance = va_arg (*args, u32);
65 return format (s, "pppoe_session%d", dev_instance);
69 dummy_interface_tx (vlib_main_t * vm,
70 vlib_node_runtime_t * node, vlib_frame_t * frame)
72 clib_warning ("you shouldn't be here, leaking buffers...");
73 return frame->n_vectors;
77 pppoe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
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);
83 return /* no error */ 0;
87 VNET_DEVICE_CLASS (pppoe_device_class,static) = {
89 .format_device_name = format_pppoe_name,
90 .tx_function = dummy_interface_tx,
91 .admin_up_down_function = pppoe_interface_admin_up_down,
96 format_pppoe_header_with_length (u8 * s, va_list * args)
98 u32 dev_instance = va_arg (*args, u32);
99 s = format (s, "unimplemented dev %u", dev_instance);
104 pppoe_build_rewrite (vnet_main_t * vnm,
106 vnet_link_t link_type, const void *dst_address)
108 int len = sizeof (pppoe_header_t) + sizeof (ethernet_header_t);
109 pppoe_main_t *pem = &pppoe_main;
114 session_id = pem->session_index_by_sw_if_index[sw_if_index];
115 t = pool_elt_at_index (pem->sessions, session_id);
117 vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES);
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);
124 pppoe_header_t *pppoe = (pppoe_header_t *) (eth_hdr + 1);
125 pppoe->ver_type = PPPOE_VER_TYPE;
127 pppoe->session_id = clib_host_to_net_u16 (t->session_id);
128 pppoe->length = 0; /* To be filled in at run-time */
133 pppoe->ppp_proto = clib_host_to_net_u16 (PPP_PROTOCOL_ip4);
136 pppoe->ppp_proto = clib_host_to_net_u16 (PPP_PROTOCOL_ip6);
146 * @brief Fixup the adj rewrite post encap. Insert the packet's length
149 pppoe_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0)
151 pppoe_header_t *pppoe0;
153 pppoe0 = vlib_buffer_get_current (b0) + sizeof (ethernet_header_t);
155 pppoe0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0)
156 - sizeof (pppoe_header_t)
157 + sizeof (pppoe0->ppp_proto)
158 - sizeof (ethernet_header_t));
162 pppoe_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
164 pppoe_main_t *pem = &pppoe_main;
165 dpo_id_t dpo = DPO_INVALID;
170 ASSERT (ADJ_INDEX_INVALID != ai);
174 switch (adj->lookup_next_index)
176 case IP_LOOKUP_NEXT_ARP:
177 case IP_LOOKUP_NEXT_GLEAN:
178 adj_nbr_midchain_update_rewrite (ai, pppoe_fixup,
180 pppoe_build_rewrite (vnm,
185 case IP_LOOKUP_NEXT_MCAST:
187 * Construct a partial rewrite from the known ethernet mcast dest MAC
188 * There's no MAC fixup, so the last 2 parameters are 0
190 adj_mcast_midchain_update_rewrite (ai, pppoe_fixup,
192 pppoe_build_rewrite (vnm,
198 case IP_LOOKUP_NEXT_DROP:
199 case IP_LOOKUP_NEXT_PUNT:
200 case IP_LOOKUP_NEXT_LOCAL:
201 case IP_LOOKUP_NEXT_REWRITE:
202 case IP_LOOKUP_NEXT_MIDCHAIN:
203 case IP_LOOKUP_NEXT_MCAST_MIDCHAIN:
204 case IP_LOOKUP_NEXT_ICMP_ERROR:
205 case IP_LOOKUP_N_NEXT:
210 session_id = pem->session_index_by_sw_if_index[sw_if_index];
211 t = pool_elt_at_index (pem->sessions, session_id);
212 interface_tx_dpo_add_or_lock (vnet_link_to_dpo_proto (adj->ia_link),
213 t->encap_if_index, &dpo);
215 adj_nbr_midchain_stack (ai, &dpo);
221 VNET_HW_INTERFACE_CLASS (pppoe_hw_class) =
224 .format_header = format_pppoe_header_with_length,
225 .build_rewrite = pppoe_build_rewrite,
226 .update_adjacency = pppoe_update_adj,
227 .flags = VNET_HW_INTERFACE_CLASS_FLAG_P2P,
231 #define foreach_copy_field \
238 pppoe_decap_next_is_valid (pppoe_main_t * pem, u32 is_ip6,
241 vlib_main_t *vm = pem->vlib_main;
242 u32 input_idx = (!is_ip6) ? ip4_input_node.index : ip6_input_node.index;
243 vlib_node_runtime_t *r = vlib_node_get_runtime (vm, input_idx);
245 return decap_fib_index < r->n_next_nodes;
248 int vnet_pppoe_add_del_session
249 (vnet_pppoe_add_del_session_args_t * a, u32 * sw_if_indexp)
251 pppoe_main_t *pem = &pppoe_main;
252 pppoe_session_t *t = 0;
253 vnet_main_t *vnm = pem->vnet_main;
254 u32 hw_if_index = ~0;
255 u32 sw_if_index = ~0;
256 u32 is_ip6 = a->is_ip6;
257 pppoe_entry_key_t cached_key;
258 pppoe_entry_result_t cached_result;
260 pppoe_entry_key_t key;
261 pppoe_entry_result_t result;
262 vnet_hw_interface_t *hi;
263 vnet_sw_interface_t *si;
267 cached_result.raw = ~0; /* warning be gone */
268 memset (&pfx, 0, sizeof (pfx));
272 pfx.fp_addr.ip4.as_u32 = a->client_ip.ip4.as_u32;
274 pfx.fp_proto = FIB_PROTOCOL_IP4;
278 pfx.fp_addr.ip6.as_u64[0] = a->client_ip.ip6.as_u64[0];
279 pfx.fp_addr.ip6.as_u64[1] = a->client_ip.ip6.as_u64[1];
281 pfx.fp_proto = FIB_PROTOCOL_IP6;
284 /* Get encap_if_index and local mac address from link_table */
285 pppoe_lookup_1 (&pem->link_table, &cached_key, &cached_result,
286 a->client_mac, 0, &key, &bucket, &result);
287 a->encap_if_index = result.fields.sw_if_index;
289 if (a->encap_if_index == ~0)
290 return VNET_API_ERROR_INVALID_SW_IF_INDEX;
292 si = vnet_get_sw_interface (vnm, a->encap_if_index);
293 hi = vnet_get_hw_interface (vnm, si->hw_if_index);
295 /* lookup session_table */
296 pppoe_lookup_1 (&pem->session_table, &cached_key, &cached_result,
297 a->client_mac, clib_host_to_net_u16 (a->session_id),
298 &key, &bucket, &result);
300 /* learn client session */
301 pppoe_learn_process (&pem->session_table, a->encap_if_index,
302 &key, &cached_key, &bucket, &result);
306 /* adding a session: session must not already exist */
307 if (result.fields.session_index != ~0)
308 return VNET_API_ERROR_TUNNEL_EXIST;
310 /*if not set explicitly, default to ip4 */
311 if (!pppoe_decap_next_is_valid (pem, is_ip6, a->decap_fib_index))
312 return VNET_API_ERROR_INVALID_DECAP_NEXT;
314 pool_get_aligned (pem->sessions, t, CLIB_CACHE_LINE_BYTES);
315 memset (t, 0, sizeof (*t));
317 clib_memcpy (t->local_mac, hi->hw_address, 6);
319 /* copy from arg structure */
320 #define _(x) t->x = a->x;
324 clib_memcpy (t->client_mac, a->client_mac, 6);
326 /* update pppoe fib with session_index */
327 result.fields.session_index = t - pem->sessions;
328 pppoe_update_1 (&pem->session_table,
329 a->client_mac, clib_host_to_net_u16 (a->session_id),
330 &key, &bucket, &result);
332 vnet_hw_interface_t *hi;
333 if (vec_len (pem->free_pppoe_session_hw_if_indices) > 0)
335 vnet_interface_main_t *im = &vnm->interface_main;
336 hw_if_index = pem->free_pppoe_session_hw_if_indices
337 [vec_len (pem->free_pppoe_session_hw_if_indices) - 1];
338 _vec_len (pem->free_pppoe_session_hw_if_indices) -= 1;
340 hi = vnet_get_hw_interface (vnm, hw_if_index);
341 hi->dev_instance = t - pem->sessions;
342 hi->hw_instance = hi->dev_instance;
344 /* clear old stats of freed session before reuse */
345 sw_if_index = hi->sw_if_index;
346 vnet_interface_counter_lock (im);
347 vlib_zero_combined_counter
348 (&im->combined_sw_if_counters[VNET_INTERFACE_COUNTER_TX],
350 vlib_zero_combined_counter (&im->combined_sw_if_counters
351 [VNET_INTERFACE_COUNTER_RX],
353 vlib_zero_simple_counter (&im->sw_if_counters
354 [VNET_INTERFACE_COUNTER_DROP],
356 vnet_interface_counter_unlock (im);
360 hw_if_index = vnet_register_interface
361 (vnm, pppoe_device_class.index, t - pem->sessions,
362 pppoe_hw_class.index, t - pem->sessions);
363 hi = vnet_get_hw_interface (vnm, hw_if_index);
366 t->hw_if_index = hw_if_index;
367 t->sw_if_index = sw_if_index = hi->sw_if_index;
369 vec_validate_init_empty (pem->session_index_by_sw_if_index, sw_if_index,
371 pem->session_index_by_sw_if_index[sw_if_index] = t - pem->sessions;
373 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, sw_if_index);
374 si->flags &= ~VNET_SW_INTERFACE_FLAG_HIDDEN;
375 vnet_sw_interface_set_flags (vnm, sw_if_index,
376 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
378 /* add reverse route for client ip */
379 fib_table_entry_path_add (a->decap_fib_index, &pfx,
380 FIB_SOURCE_PLUGIN_HI, FIB_ENTRY_FLAG_NONE,
381 fib_proto_to_dpo (pfx.fp_proto),
382 &pfx.fp_addr, sw_if_index, ~0,
383 1, NULL, FIB_ROUTE_PATH_FLAG_NONE);
388 /* deleting a session: session must exist */
389 if (result.fields.session_index == ~0)
390 return VNET_API_ERROR_NO_SUCH_ENTRY;
392 t = pool_elt_at_index (pem->sessions, result.fields.session_index);
393 sw_if_index = t->sw_if_index;
395 vnet_sw_interface_set_flags (vnm, t->sw_if_index, 0 /* down */ );
396 vnet_sw_interface_t *si = vnet_get_sw_interface (vnm, t->sw_if_index);
397 si->flags |= VNET_SW_INTERFACE_FLAG_HIDDEN;
399 vec_add1 (pem->free_pppoe_session_hw_if_indices, t->hw_if_index);
401 pem->session_index_by_sw_if_index[t->sw_if_index] = ~0;
403 /* update pppoe fib with session_inde=~0x */
404 result.fields.session_index = ~0;
405 pppoe_update_1 (&pem->session_table,
406 a->client_mac, clib_host_to_net_u16 (a->session_id),
407 &key, &bucket, &result);
410 /* delete reverse route for client ip */
411 fib_table_entry_path_remove (a->decap_fib_index, &pfx,
412 FIB_SOURCE_PLUGIN_HI,
413 fib_proto_to_dpo (pfx.fp_proto),
416 FIB_ROUTE_PATH_FLAG_NONE);
418 pool_put (pem->sessions, t);
422 *sw_if_indexp = sw_if_index;
427 static clib_error_t *
428 pppoe_add_del_session_command_fn (vlib_main_t * vm,
429 unformat_input_t * input,
430 vlib_cli_command_t * cmd)
432 unformat_input_t _line_input, *line_input = &_line_input;
434 ip46_address_t client_ip;
436 u8 client_ip_set = 0;
439 u32 encap_if_index = 0;
440 u32 decap_fib_index = 0;
441 u8 client_mac[6] = { 0 };
442 u8 client_mac_set = 0;
445 vnet_pppoe_add_del_session_args_t _a, *a = &_a;
446 u32 session_sw_if_index;
447 clib_error_t *error = NULL;
449 /* Cant "universally zero init" (={0}) due to GCC bug 53119 */
450 memset (&client_ip, 0, sizeof client_ip);
452 /* Get a line of input. */
453 if (!unformat_user (input, unformat_line_input, line_input))
456 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
458 if (unformat (line_input, "del"))
462 else if (unformat (line_input, "session-id %d", &session_id))
464 else if (unformat (line_input, "client-ip %U",
465 unformat_ip4_address, &client_ip.ip4))
470 else if (unformat (line_input, "client-ip %U",
471 unformat_ip6_address, &client_ip.ip6))
476 else if (unformat (line_input, "decap-vrf-id %d", &tmp))
479 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP6, tmp);
481 decap_fib_index = fib_table_find (FIB_PROTOCOL_IP4, tmp);
483 if (decap_fib_index == ~0)
486 clib_error_return (0, "nonexistent decap fib id %d", tmp);
492 (line_input, "client-mac %U", unformat_ethernet_address,
497 error = clib_error_return (0, "parse error: '%U'",
498 format_unformat_error, line_input);
503 if (client_ip_set == 0)
506 clib_error_return (0, "session client ip address not specified");
510 if (ipv4_set && ipv6_set)
512 error = clib_error_return (0, "both IPv4 and IPv6 addresses specified");
516 if (client_mac_set == 0)
518 error = clib_error_return (0, "session client mac not specified");
522 memset (a, 0, sizeof (*a));
525 a->is_ip6 = ipv6_set;
527 #define _(x) a->x = x;
531 clib_memcpy (a->client_mac, client_mac, 6);
533 rv = vnet_pppoe_add_del_session (a, &session_sw_if_index);
539 vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
540 vnet_get_main (), session_sw_if_index);
543 case VNET_API_ERROR_TUNNEL_EXIST:
544 error = clib_error_return (0, "session already exists...");
547 case VNET_API_ERROR_NO_SUCH_ENTRY:
548 error = clib_error_return (0, "session does not exist...");
552 error = clib_error_return
553 (0, "vnet_pppoe_add_del_session returned %d", rv);
558 unformat_free (line_input);
564 * Add or delete a PPPPOE Session.
567 * Example of how to create a PPPPOE Session:
568 * @cliexcmd{create pppoe session client-ip 10.0.3.1 session-id 13
569 * client-mac 00:01:02:03:04:05 }
570 * Example of how to delete a PPPPOE Session:
571 * @cliexcmd{create pppoe session client-ip 10.0.3.1 session-id 13
572 * client-mac 00:01:02:03:04:05 del }
575 VLIB_CLI_COMMAND (create_pppoe_session_command, static) = {
576 .path = "create pppoe session",
578 "create pppoe session client-ip <client-ip> session-id <nn>"
579 " client-mac <client-mac> [decap-vrf-id <nn>] [del]",
580 .function = pppoe_add_del_session_command_fn,
585 static clib_error_t *
586 show_pppoe_session_command_fn (vlib_main_t * vm,
587 unformat_input_t * input,
588 vlib_cli_command_t * cmd)
590 pppoe_main_t *pem = &pppoe_main;
593 if (pool_elts (pem->sessions) == 0)
594 vlib_cli_output (vm, "No pppoe sessions configured...");
596 pool_foreach (t, pem->sessions,
598 vlib_cli_output (vm, "%U",format_pppoe_session, t);
606 * Display all the PPPPOE Session entries.
609 * Example of how to display the PPPPOE Session entries:
610 * @cliexstart{show pppoe session}
611 * [0] client-ip 10.0.3.1 session_id 13 encap-if-index 0 decap-vrf-id 13 sw_if_index 5
612 * local-mac a0:b0:c0:d0:e0:f0 client-mac 00:01:02:03:04:05
616 VLIB_CLI_COMMAND (show_pppoe_session_command, static) = {
617 .path = "show pppoe session",
618 .short_help = "show pppoe session",
619 .function = show_pppoe_session_command_fn,
623 /** Display the contents of the PPPoE Fib. */
624 static clib_error_t *
625 show_pppoe_fib_command_fn (vlib_main_t * vm,
626 unformat_input_t * input, vlib_cli_command_t * cmd)
628 pppoe_main_t *pem = &pppoe_main;
629 BVT (clib_bihash) * h = &pem->session_table;
630 BVT (clib_bihash_bucket) * b;
631 BVT (clib_bihash_value) * v;
632 pppoe_entry_key_t key;
633 pppoe_entry_result_t result;
635 u64 total_entries = 0;
639 for (i = 0; i < h->nbuckets; i++)
644 v = BV (clib_bihash_get_value) (h, b->offset);
645 for (j = 0; j < (1 << b->log2_pages); j++)
647 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
649 if (v->kvp[k].key == ~0ULL && v->kvp[k].value == ~0ULL)
656 "%=19s%=12s%=13s%=14s",
657 "Mac-Address", "session_id", "sw_if_index",
661 key.raw = v->kvp[k].key;
662 result.raw = v->kvp[k].value;
666 "%=19U%=12d%=13d%=14d",
667 format_ethernet_address, key.fields.mac,
668 clib_net_to_host_u16 (key.fields.session_id),
669 result.fields.sw_if_index == ~0
670 ? -1 : result.fields.sw_if_index,
671 result.fields.session_index == ~0
672 ? -1 : result.fields.session_index);
673 vec_reset_length (s);
680 if (total_entries == 0)
681 vlib_cli_output (vm, "no pppoe fib entries");
683 vlib_cli_output (vm, "%lld pppoe fib entries", total_entries);
690 * This command dispays the MAC Address entries of the PPPoE FIB table.
691 * Output can be filtered to just get the number of MAC Addresses or display
695 * Example of how to display the number of MAC Address entries in the PPPoE
697 * @cliexstart{show pppoe fib}
698 * Mac Address session_id Interface sw_if_index session_index
699 * 52:54:00:53:18:33 1 GigabitEthernet0/8/0 2 0
700 * 52:54:00:53:18:55 2 GigabitEthernet0/8/1 3 1
704 VLIB_CLI_COMMAND (show_pppoe_fib_command, static) = {
705 .path = "show pppoe fib",
706 .short_help = "show pppoe fib",
707 .function = show_pppoe_fib_command_fn,
712 pppoe_init (vlib_main_t * vm)
714 pppoe_main_t *pem = &pppoe_main;
716 pem->vnet_main = vnet_get_main ();
719 /* Create the hash table */
720 BV (clib_bihash_init) (&pem->link_table, "pppoe link table",
721 PPPOE_NUM_BUCKETS, PPPOE_MEMORY_SIZE);
723 BV (clib_bihash_init) (&pem->session_table, "pppoe session table",
724 PPPOE_NUM_BUCKETS, PPPOE_MEMORY_SIZE);
726 ethernet_register_input_type (vm, ETHERNET_TYPE_PPPOE_SESSION,
727 pppoe_input_node.index);
729 ethernet_register_input_type (vm, ETHERNET_TYPE_PPPOE_DISCOVERY,
730 pppoe_cp_dispatch_node.index);
735 VLIB_INIT_FUNCTION (pppoe_init);
738 VLIB_PLUGIN_REGISTER () = {
739 .version = VPP_BUILD_VER,
740 .description = "PPPoE",
745 * fd.io coding-style-patch-verification: ON
748 * eval: (c-set-style "gnu")