2 * Copyright (c) 2015 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vnet/vnet.h>
16 #include <vnet/ip/ip.h>
17 #include <vnet/ethernet/ethernet.h>
20 #include <vnet/devices/pci/ixge.h>
22 #include <vnet/devices/dpdk/dpdk.h>
25 #include <vppinfra/error.h>
26 #include <vppinfra/hash.h>
32 * get_interface_ethernet_address
33 * paints the ethernet address for a given interface
34 * into the supplied destination
37 get_interface_ethernet_address (l2t_main_t * lm, u8 * dst, u32 sw_if_index)
39 ethernet_main_t *em = ethernet_get_main (lm->vlib_main);
40 ethernet_interface_t *ei;
41 vnet_hw_interface_t *hi;
43 hi = vnet_get_sup_hw_interface (lm->vnet_main, sw_if_index);
44 ei = pool_elt_at_index (em->interfaces, hi->hw_instance);
45 clib_memcpy (dst, ei->address, sizeof (ei->address));
48 /* packet trace format function */
50 format_l2t_trace (u8 * s, va_list * args)
52 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
53 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
54 l2t_trace_t *t = va_arg (*args, l2t_trace_t *);
56 if (t->is_user_to_network)
57 s = format (s, "L2T: %U (client) -> %U (our) session %d",
58 format_ip6_address, &t->client_address,
59 format_ip6_address, &t->our_address, t->session_index);
61 s = format (s, "L2T: %U (our) -> %U (client) session %d)",
62 format_ip6_address, &t->our_address,
63 format_ip6_address, &t->client_address, t->session_index);
68 format_l2t_session (u8 * s, va_list * args)
70 l2t_session_t *session = va_arg (*args, l2t_session_t *);
71 l2t_main_t *lm = &l2t_main;
75 s = format (s, "[%d] %U (our) %U (client) vlan-id %d rx_sw_if_index %d\n",
76 session - lm->sessions,
77 format_ip6_address, &session->our_address,
78 format_ip6_address, &session->client_address,
79 clib_net_to_host_u16 (session->vlan_id), session->sw_if_index);
81 s = format (s, " local cookie %llx remote cookie %llx\n",
82 clib_net_to_host_u64 (session->local_cookie),
83 clib_net_to_host_u64 (session->remote_cookie));
85 if (session->cookie_flags & L2TP_COOKIE_ROLLOVER_LOCAL)
87 s = format (s, " local rollover cookie %llx\n",
88 clib_net_to_host_u64 (session->lcl_ro_cookie));
91 s = format (s, " local session-id %d remote session-id %d\n",
92 clib_net_to_host_u32 (session->local_session_id),
93 clib_net_to_host_u32 (session->remote_session_id));
95 s = format (s, " l2 specific sublayer %s\n",
96 session->l2_sublayer_present ? "preset" : "absent");
99 session_index_to_counter_index (session - lm->sessions,
100 SESSION_COUNTER_USER_TO_NETWORK);
102 vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
104 s = format (s, " user-to-net: %llu pkts %llu bytes\n",
107 vlib_get_combined_counter (&lm->counter_main, counter_index + 1, &v);
110 s = format (s, " net-to-user: %llu pkts %llu bytes\n",
115 static clib_error_t *
116 show_session_summary_command_fn (vlib_main_t * vm,
117 unformat_input_t * input,
118 vlib_cli_command_t * cmd)
120 l2t_main_t *lm = &l2t_main;
122 vlib_cli_output (vm, "%d active sessions\n", pool_elts (lm->sessions));
128 static VLIB_CLI_COMMAND (show_session_summary_command) = {
129 .path = "show session",
130 .short_help = "show session summary",
131 .function = show_session_summary_command_fn,
135 static clib_error_t *
136 show_session_detail_command_fn (vlib_main_t * vm,
137 unformat_input_t * input,
138 vlib_cli_command_t * cmd)
140 l2t_session_t *session;
141 l2t_main_t *lm = &l2t_main;
144 pool_foreach (session, lm->sessions,
146 vlib_cli_output (vm, "%U", format_l2t_session, session);
154 static VLIB_CLI_COMMAND (show_session_detail_command) = {
155 .path = "show session detail",
156 .short_help = "show session table detail",
157 .function = show_session_detail_command_fn,
161 static clib_error_t *
162 test_counters_command_fn (vlib_main_t * vm,
163 unformat_input_t * input, vlib_cli_command_t * cmd)
165 l2t_session_t *session;
166 l2t_main_t *lm = &l2t_main;
172 pool_foreach (session, lm->sessions,
174 session_index = session - lm->sessions;
176 session_index_to_counter_index (session_index,
177 SESSION_COUNTER_USER_TO_NETWORK);
178 vlib_increment_combined_counter (&lm->counter_main,
180 1/*pkt*/, 1111 /*bytes*/);
181 vlib_increment_combined_counter (&lm->counter_main,
183 1/*pkt*/, 2222 /*bytes*/);
187 vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
193 static VLIB_CLI_COMMAND (test_counters_command) = {
194 .path = "test counters",
195 .short_help = "increment all active counters",
196 .function = test_counters_command_fn,
200 static clib_error_t *
201 clear_counters_command_fn (vlib_main_t * vm,
202 unformat_input_t * input, vlib_cli_command_t * cmd)
204 l2t_session_t *session;
205 l2t_main_t *lm = &l2t_main;
211 pool_foreach (session, lm->sessions,
213 session_index = session - lm->sessions;
215 session_index_to_counter_index (session_index,
216 SESSION_COUNTER_USER_TO_NETWORK);
217 vlib_zero_combined_counter (&lm->counter_main, counter_index);
218 vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
222 vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
228 static VLIB_CLI_COMMAND (clear_counters_command) = {
229 .path = "clear counters",
230 .short_help = "clear all active counters",
231 .function = clear_counters_command_fn,
235 static clib_error_t *
236 l2tp_session_add_command_fn (vlib_main_t * vm,
237 unformat_input_t * input,
238 vlib_cli_command_t * cmd)
240 ip6_address_t client_address, our_address;
241 ip6_address_t *dst_address_copy, *src_address_copy;
242 unformat_input_t _line_input, *line_input = &_line_input;
244 u32 sw_if_index = (u32) ~ 0;
245 l2t_main_t *lm = &l2t_main;
248 vnet_hw_interface_t *hi;
249 vnet_sw_interface_t *si;
251 uword vlan_and_sw_if_index_key;
253 u64 local_cookie = (u64) ~ 0, remote_cookie = (u64) ~ 0;
254 u32 local_session_id = 1, remote_session_id = 1;
255 int our_address_set = 0, client_address_set = 0;
256 int l2_sublayer_present = 0;
258 /* Get a line of input. */
259 if (!unformat_user (input, unformat_line_input, line_input))
262 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
264 if (unformat (line_input, "client %U",
265 unformat_ip6_address, &client_address))
266 client_address_set = 1;
267 else if (unformat (line_input, "our %U",
268 unformat_ip6_address, &our_address))
270 else if (unformat (line_input, "vlan %d", &vlan_id))
272 else if (unformat (line_input, "l2-interface %U",
273 unformat_vnet_sw_interface,
274 vnet_get_main (), &sw_if_index))
276 else if (unformat (line_input, "interface %U",
277 unformat_vnet_sw_interface,
278 vnet_get_main (), &sw_if_index))
280 else if (unformat (line_input, "local-cookie %llx", &local_cookie))
282 else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
284 else if (unformat (line_input, "local-session-id %d",
287 else if (unformat (line_input, "remote-session-id %d",
290 else if (unformat (line_input, "l2-sublayer-present"))
291 l2_sublayer_present = 1;
293 return clib_error_return (0, "parse error: '%U'",
294 format_unformat_error, line_input);
297 unformat_free (line_input);
299 if (sw_if_index == (u32) ~ 0)
300 return clib_error_return (0, "l2-interface not specified");
301 if (our_address_set == 0)
302 return clib_error_return (0, "our address not specified");
303 if (client_address_set == 0)
304 return clib_error_return (0, "client address not specified");
306 remote_session_id = clib_host_to_net_u32 (remote_session_id);
307 local_session_id = clib_host_to_net_u32 (local_session_id);
309 switch (lm->lookup_type)
311 case L2T_LOOKUP_SRC_ADDRESS:
312 p = hash_get_mem (lm->session_by_src_address, &client_address);
314 return clib_error_return
315 (0, "Session w/ client address %U already exists",
316 format_ip6_address, &client_address);
319 case L2T_LOOKUP_DST_ADDRESS:
320 p = hash_get_mem (lm->session_by_dst_address, &our_address);
322 return clib_error_return
323 (0, "Session w/ our address %U already exists",
324 format_ip6_address, &our_address);
327 case L2T_LOOKUP_SESSION_ID:
328 p = hash_get (lm->session_by_session_id, local_session_id);
330 return clib_error_return
332 "Session w/ local session id %d already exists",
333 clib_net_to_host_u32 (local_session_id));
340 pool_get (lm->sessions, s);
341 memset (s, 0, sizeof (*s));
342 clib_memcpy (&s->our_address, &our_address, sizeof (s->our_address));
343 clib_memcpy (&s->client_address, &client_address,
344 sizeof (s->client_address));
345 s->sw_if_index = sw_if_index;
346 s->vlan_id = clib_host_to_net_u16 (vlan_id);
347 s->local_cookie = clib_host_to_net_u64 (local_cookie);
348 l2tp_session_set_remote_cookie (s, remote_cookie);
349 s->local_session_id = local_session_id;
350 s->remote_session_id = remote_session_id;
351 s->l2_sublayer_present = l2_sublayer_present;
353 hi = vnet_get_sup_hw_interface (lm->vnet_main, sw_if_index);
354 si = vnet_get_sup_sw_interface (lm->vnet_main, sw_if_index);
356 next_index = vlib_node_add_next (vm, l2t_ip6_node.index,
357 hi->output_node_index);
358 s->l2_output_next_index = next_index;
359 s->l2_output_sw_if_index = si->sw_if_index;
361 /* Setup hash table entries */
362 switch (lm->lookup_type)
364 case L2T_LOOKUP_SRC_ADDRESS:
365 src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
366 clib_memcpy (src_address_copy, &client_address,
367 sizeof (*src_address_copy));
368 hash_set_mem (lm->session_by_src_address, src_address_copy,
371 case L2T_LOOKUP_DST_ADDRESS:
372 dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
373 clib_memcpy (dst_address_copy, &our_address,
374 sizeof (*dst_address_copy));
375 hash_set_mem (lm->session_by_dst_address, dst_address_copy,
378 case L2T_LOOKUP_SESSION_ID:
379 hash_set (lm->session_by_session_id, local_session_id,
387 vlan_and_sw_if_index_key = ((uword) (s->vlan_id) << 32) | sw_if_index;
388 hash_set (lm->session_by_vlan_and_rx_sw_if_index,
389 vlan_and_sw_if_index_key, s - lm->sessions);
391 /* validate counters */
393 session_index_to_counter_index (s - lm->sessions,
394 SESSION_COUNTER_USER_TO_NETWORK);
395 vlib_validate_counter (&lm->counter_main, counter_index);
396 vlib_validate_counter (&lm->counter_main, counter_index + 1);
398 /* Set promiscuous mode on the l2 interface */
399 ethernet_set_flags (lm->vnet_main, hi->hw_if_index,
400 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
401 vnet_hw_interface_rx_redirect_to_node (lm->vnet_main, hi->hw_if_index,
407 static VLIB_CLI_COMMAND (l2tp_session_add_command) = {
408 .path = "l2tp session add",
410 "l2tp session add client <ip6> our <ip6> vlan <id> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec> l2-interface <int>",
411 .function = l2tp_session_add_command_fn,
415 static clib_error_t *
416 l2tp_session_del_command_fn (vlib_main_t * vm,
417 unformat_input_t * input,
418 vlib_cli_command_t * cmd)
420 l2t_main_t *lm = &l2t_main;
425 uword vlan_and_sw_if_index_key;
427 if (!unformat (input, "%d", &session_index))
428 return clib_error_return (0, "missing session index: '%U'",
429 format_unformat_error, input);
431 if (pool_is_free_index (lm->sessions, session_index))
432 return clib_error_return (0, "session %d not in use", session_index);
434 s = pool_elt_at_index (lm->sessions, session_index);
436 switch (lm->lookup_type)
438 case L2T_LOOKUP_SRC_ADDRESS:
439 hp = hash_get_pair_mem (lm->session_by_src_address, &s->client_address);
442 key = (void *) (hp->key);
443 hash_unset_mem (lm->session_by_src_address, &s->client_address);
447 clib_warning ("session %d src address key %U AWOL",
449 format_ip6_address, &s->client_address);
452 case L2T_LOOKUP_DST_ADDRESS:
453 hp = hash_get_pair_mem (lm->session_by_dst_address, &s->our_address);
456 key = (void *) (hp->key);
457 hash_unset_mem (lm->session_by_dst_address, &s->our_address);
461 clib_warning ("session %d dst address key %U AWOL",
462 s - lm->sessions, format_ip6_address, &s->our_address);
465 case L2T_LOOKUP_SESSION_ID:
466 hash_unset (lm->session_by_session_id, s->local_session_id);
473 vlan_and_sw_if_index_key = ((uword) (s->vlan_id) << 32) | s->sw_if_index;
475 hash_unset (lm->session_by_vlan_and_rx_sw_if_index,
476 vlan_and_sw_if_index_key);
478 pool_put (lm->sessions, s);
483 static VLIB_CLI_COMMAND (l2tp_session_del_command) = {
484 .path = "l2tp session delete",
486 "l2tp session delete <session-id>",
487 .function = l2tp_session_del_command_fn,
491 static clib_error_t *
492 l2tp_session_cookie_command_fn (vlib_main_t * vm,
493 unformat_input_t * input,
494 vlib_cli_command_t * cmd)
496 l2t_main_t *lm = &l2t_main;
499 u64 lcl_ro_cookie = (u64) ~ 0, rem_ro_cookie = (u64) ~ 0;
502 if (!unformat (input, "%d", &session_index))
503 return clib_error_return (0, "missing session index: '%U'",
504 format_unformat_error, input);
506 if (pool_is_free_index (lm->sessions, session_index))
507 return clib_error_return (0, "session %d not in use", session_index);
509 s = pool_elt_at_index (lm->sessions, session_index);
511 if (unformat (input, "commit"))
513 if (!s->cookie_flags)
515 return clib_error_return (0, "no rollover cookie ready to commit");
519 l2tp_session_cookie_commit (s);
523 if (!unformat (input, "rollover"))
524 return clib_error_return (0, "missing 'commit|rollover': '%U'",
525 format_unformat_error, input);
526 if (unformat (input, "local %llx", &lcl_ro_cookie))
528 cookie_flags |= L2TP_COOKIE_ROLLOVER_LOCAL;
529 l2tp_session_set_local_rollover_cookie (s, lcl_ro_cookie);
531 if (unformat (input, "remote %llx", &rem_ro_cookie))
533 cookie_flags |= L2TP_COOKIE_ROLLOVER_REMOTE;
534 l2tp_session_set_remote_cookie (s, rem_ro_cookie);
537 return clib_error_return (0, "no rollover cookie specified");
543 static VLIB_CLI_COMMAND (l2tp_session_cookie_command) = {
544 .path = "l2tp session cookie",
546 "l2tp session cookie <session id> commit|rollover [local <hex>] [remote <hex>]",
547 .function = l2tp_session_cookie_command_fn,
552 * fd.io coding-style-patch-verification: ON
555 * eval: (c-set-style "gnu")