/* *------------------------------------------------------------------ * Copyright (c) 2017 Intel and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *------------------------------------------------------------------ */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include pppoe_main_t pppoe_main; u8 * format_pppoe_session (u8 * s, va_list * args) { pppoe_session_t *t = va_arg (*args, pppoe_session_t *); pppoe_main_t *pem = &pppoe_main; s = format (s, "[%d] sw-if-index %d client-ip %U session-id %d ", t - pem->sessions, t->sw_if_index, format_ip46_address, &t->client_ip, IP46_TYPE_ANY, t->session_id); s = format (s, "encap-if-index %d decap-fib-index %d\n", t->encap_if_index, t->decap_fib_index); s = format (s, " local-mac %U client-mac %U", format_ethernet_address, t->local_mac, format_ethernet_address, t->client_mac); return s; } static u8 * format_pppoe_name (u8 * s, va_list * args) { u32 dev_instance = va_arg (*args, u32); return format (s, "pppoe_session%d", dev_instance); } static uword dummy_interface_tx (vlib_main_t * vm, vlib_node_runtime_t * node, vlib_frame_t * frame) { clib_warning ("you shouldn't be here, leaking buffers..."); return frame->n_vectors; } static clib_error_t * pppoe_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags) { u32 hw_flags = (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP) ? VNET_HW_INTERFACE_FLAG_LINK_UP : 0; vnet_hw_interface_set_flags (vnm, hw_if_index, hw_flags); return /* no error */ 0; } /* *INDENT-OFF* */ VNET_DEVICE_CLASS (pppoe_device_class,static) = { .name = "PPPoE", .format_device_name = format_pppoe_name, .tx_function = dummy_interface_tx, .admin_up_down_function = pppoe_interface_admin_up_down, }; /* *INDENT-ON* */ static u8 * format_pppoe_header_with_length (u8 * s, va_list * args) { u32 dev_instance = va_arg (*args, u32); s = format (s, "unimplemented dev %u", dev_instance); return s; } static u8 * pppoe_build_rewrite (vnet_main_t * vnm, u32 sw_if_index, vnet_link_t link_type, const void *dst_address) { int len = sizeof (pppoe_header_t) + sizeof (ethernet_header_t); pppoe_main_t *pem = &pppoe_main; pppoe_session_t *t; u32 session_id; u8 *rw = 0; session_id = pem->session_index_by_sw_if_index[sw_if_index]; t = pool_elt_at_index (pem->sessions, session_id); vec_validate_aligned (rw, len - 1, CLIB_CACHE_LINE_BYTES); ethernet_header_t *eth_hdr = (ethernet_header_t *) rw; clib_memcpy (eth_hdr->dst_address, t->client_mac, 6); clib_memcpy (eth_hdr->src_address, t->local_mac, 6); eth_hdr->type = clib_host_to_net_u16 (ETHERNET_TYPE_PPPOE_SESSION); pppoe_header_t *pppoe = (pppoe_header_t *) (eth_hdr + 1); pppoe->ver_type = PPPOE_VER_TYPE; pppoe->code = 0; pppoe->session_id = clib_host_to_net_u16 (t->session_id); pppoe->length = 0; /* To be filled in at run-time */ switch (link_type) { case VNET_LINK_IP4: pppoe->ppp_proto = clib_host_to_net_u16 (PPP_PROTOCOL_ip4); break; case VNET_LINK_IP6: pppoe->ppp_proto = clib_host_to_net_u16 (PPP_PROTOCOL_ip6); break; default: break; } return rw; } /** * @brief Fixup the adj rewrite post encap. Insert the packet's length */ static void pppoe_fixup (vlib_main_t * vm, ip_adjacency_t * adj, vlib_buffer_t * b0, const void *data) { const pppoe_session_t *t; pppoe_header_t *pppoe0; /* update the rewrite string */ pppoe0 = vlib_buffer_get_current (b0) + sizeof (ethernet_header_t); pppoe0->length = clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) - sizeof (pppoe_header_t) + sizeof (pppoe0->ppp_proto) - sizeof (ethernet_header_t)); /* Swap to the the packet's output interface to the encap (not the * sessio
DisableFormat: true
SortIncludes: false
", tmp); goto done; } } else if (unformat (line_input, "client-mac %U", unformat_ethernet_address, client_mac)) client_mac_set = 1; else { error = clib_error_return (0, "parse error: '%U'", format_unformat_error, line_input); goto done; } } if (client_ip_set == 0) { error = clib_error_return (0, "session client ip address not specified"); goto done; } if (ipv4_set && ipv6_set) { error = clib_error_return (0, "both IPv4 and IPv6 addresses specified"); goto done; } if (client_mac_set == 0) { error = clib_error_return (0, "session client mac not specified"); goto done; } clib_memset (a, 0, sizeof (*a)); a->is_add = is_add; a->is_ip6 = ipv6_set; #define _(x) a->x = x; foreach_copy_field; #undef _ clib_memcpy (a->client_mac, client_mac, 6); rv = vnet_pppoe_add_del_session (a, &session_sw_if_index); switch (rv) { case 0: if (is_add) vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main (), session_sw_if_index); break; case VNET_API_ERROR_TUNNEL_EXIST: error = clib_error_return (0, "session already exists..."); goto done; case VNET_API_ERROR_NO_SUCH_ENTRY: error = clib_error_return (0, "session does not exist..."); goto done; default: error = clib_error_return (0, "vnet_pppoe_add_del_session returned %d", rv); goto done; } done: unformat_free (line_input); return error; } /*? * Add or delete a PPPoE Session. * * @cliexpar * Example of how to create a PPPoE Session: * @cliexcmd{create pppoe session client-ip 10.0.3.1 session-id 13 * client-mac 00:01:02:03:04:05 } * Example of how to delete a PPPoE Session: * @cliexcmd{create pppoe session client-ip 10.0.3.1 session-id 13 * client-mac 00:01:02:03:04:05 del } ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (create_pppoe_session_command, static) = { .path = "create pppoe session", .short_help = "create pppoe session client-ip session-id " " client-mac [decap-vrf-id ] [del]", .function = pppoe_add_del_session_command_fn, }; /* *INDENT-ON* */ /* *INDENT-OFF* */ static clib_error_t * show_pppoe_session_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { pppoe_main_t *pem = &pppoe_main; pppoe_session_t *t; if (pool_elts (pem->sessions) == 0) vlib_cli_output (vm, "No pppoe sessions configured..."); pool_foreach (t, pem->sessions, ({ vlib_cli_output (vm, "%U",format_pppoe_session, t); })); return 0; } /* *INDENT-ON* */ /*? * Display all the PPPoE Session entries. * * @cliexpar * Example of how to display the PPPoE Session entries: * @cliexstart{show pppoe session} * [0] client-ip 10.0.3.1 session_id 13 encap-if-index 0 decap-vrf-id 13 sw_if_index 5 * local-mac a0:b0:c0:d0:e0:f0 client-mac 00:01:02:03:04:05 * @cliexend ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_pppoe_session_command, static) = { .path = "show pppoe session", .short_help = "show pppoe session", .function = show_pppoe_session_command_fn, }; /* *INDENT-ON* */ typedef struct pppoe_show_walk_ctx_t_ { vlib_main_t *vm; u8 first_entry; u32 total_entries; } pppoe_show_walk_ctx_t; static void pppoe_show_walk_cb (BVT (clib_bihash_kv) * kvp, void *arg) { pppoe_show_walk_ctx_t *ctx = arg; pppoe_entry_result_t result; pppoe_entry_key_t key; if (ctx->first_entry) { ctx->first_entry = 0; vlib_cli_output (ctx->vm, "%=19s%=12s%=13s%=14s", "Mac-Address", "session_id", "sw_if_index", "session_index"); } key.raw = kvp->key; result.raw = kvp->value; vlib_cli_output (ctx->vm, "%=19U%=12d%=13d%=14d", format_ethernet_address, key.fields.mac, clib_net_to_host_u16 (key.fields.session_id), result.fields.sw_if_index == ~0 ? -1 : result.fields.sw_if_index, result.fields.session_index == ~0 ? -1 : result.fields.session_index); ctx->total_entries++; } /** Display the contents of the PPPoE Fib. */ static clib_error_t * show_pppoe_fib_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { pppoe_main_t *pem = &pppoe_main; pppoe_show_walk_ctx_t ctx = { .first_entry = 1, .vm = vm, }; BV (clib_bihash_foreach_key_value_pair) (&pem->session_table, pppoe_show_walk_cb, &ctx); if (ctx.total_entries == 0) vlib_cli_output (vm, "no pppoe fib entries"); else vlib_cli_output (vm, "%lld pppoe fib entries", ctx.total_entries); return 0; } /*? * This command dispays the MAC Address entries of the PPPoE FIB table. * Output can be filtered to just get the number of MAC Addresses or display * each MAC Address. * * @cliexpar * Example of how to display the number of MAC Address entries in the PPPoE * FIB table: * @cliexstart{show pppoe fib} * Mac Address session_id Interface sw_if_index session_index * 52:54:00:53:18:33 1 GigabitEthernet0/8/0 2 0 * 52:54:00:53:18:55 2 GigabitEthernet0/8/1 3 1 * @cliexend ?*/ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_pppoe_fib_command, static) = { .path = "show pppoe fib", .short_help = "show pppoe fib", .function = show_pppoe_fib_command_fn, }; /* *INDENT-ON* */ clib_error_t * pppoe_init (vlib_main_t * vm) { pppoe_main_t *pem = &pppoe_main; pem->vnet_main = vnet_get_main (); pem->vlib_main = vm; /* Create the hash table */ BV (clib_bihash_init) (&pem->link_table, "pppoe link table", PPPOE_NUM_BUCKETS, PPPOE_MEMORY_SIZE); BV (clib_bihash_init) (&pem->session_table, "pppoe session table", PPPOE_NUM_BUCKETS, PPPOE_MEMORY_SIZE); ethernet_register_input_type (vm, ETHERNET_TYPE_PPPOE_SESSION, pppoe_input_node.index); ethernet_register_input_type (vm, ETHERNET_TYPE_PPPOE_DISCOVERY, pppoe_cp_dispatch_node.index); return 0; } VLIB_INIT_FUNCTION (pppoe_init); /* *INDENT-OFF* */ VLIB_PLUGIN_REGISTER () = { .version = VPP_BUILD_VER, .description = "PPPoE", }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */