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
36 void get_interface_ethernet_address (l2t_main_t *lm,
37 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 */
49 u8 * format_l2t_trace (u8 * s, va_list * args)
51 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
52 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
53 l2t_trace_t * t = va_arg (*args, l2t_trace_t *);
55 if (t->is_user_to_network)
56 s = format (s, "L2T: %U (client) -> %U (our) session %d",
57 format_ip6_address, &t->client_address,
58 format_ip6_address, &t->our_address,
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,
68 u8 * 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) {
86 s = format (s, " local rollover cookie %llx\n",
87 clib_net_to_host_u64 (session->lcl_ro_cookie));
90 s = format (s, " local session-id %d remote session-id %d\n",
91 clib_net_to_host_u32 (session->local_session_id),
92 clib_net_to_host_u32 (session->remote_session_id));
94 s = format (s, " l2 specific sublayer %s\n",
95 session->l2_sublayer_present ? "preset" : "absent");
98 session_index_to_counter_index (session - lm->sessions,
99 SESSION_COUNTER_USER_TO_NETWORK);
101 vlib_get_combined_counter (&lm->counter_main, counter_index, &v);
103 s = format (s, " user-to-net: %llu pkts %llu bytes\n",
106 vlib_get_combined_counter (&lm->counter_main, counter_index+1, &v);
109 s = format (s, " net-to-user: %llu pkts %llu bytes\n",
114 static clib_error_t *
115 show_session_summary_command_fn (vlib_main_t * vm,
116 unformat_input_t * input,
117 vlib_cli_command_t * cmd)
119 l2t_main_t *lm = &l2t_main;
121 vlib_cli_output (vm, "%d active sessions\n", pool_elts (lm->sessions));
126 static VLIB_CLI_COMMAND (show_session_summary_command) = {
127 .path = "show session",
128 .short_help = "show session summary",
129 .function = show_session_summary_command_fn,
132 static clib_error_t *
133 show_session_detail_command_fn (vlib_main_t * vm,
134 unformat_input_t * input,
135 vlib_cli_command_t * cmd)
137 l2t_session_t *session;
138 l2t_main_t *lm = &l2t_main;
140 pool_foreach (session, lm->sessions,
142 vlib_cli_output (vm, "%U", format_l2t_session, session);
148 static VLIB_CLI_COMMAND (show_session_detail_command) = {
149 .path = "show session detail",
150 .short_help = "show session table detail",
151 .function = show_session_detail_command_fn,
154 static clib_error_t *
155 test_counters_command_fn (vlib_main_t * vm,
156 unformat_input_t * input,
157 vlib_cli_command_t * cmd)
159 l2t_session_t *session;
160 l2t_main_t *lm = &l2t_main;
165 pool_foreach (session, lm->sessions,
167 session_index = session - lm->sessions;
169 session_index_to_counter_index (session_index,
170 SESSION_COUNTER_USER_TO_NETWORK);
171 vlib_increment_combined_counter (&lm->counter_main,
173 1/*pkt*/, 1111 /*bytes*/);
174 vlib_increment_combined_counter (&lm->counter_main,
176 1/*pkt*/, 2222 /*bytes*/);
180 vlib_cli_output (vm, "Incremented %d active counters\n", nincr);
185 static VLIB_CLI_COMMAND (test_counters_command) = {
186 .path = "test counters",
187 .short_help = "increment all active counters",
188 .function = test_counters_command_fn,
191 static clib_error_t *
192 clear_counters_command_fn (vlib_main_t * vm,
193 unformat_input_t * input,
194 vlib_cli_command_t * cmd)
196 l2t_session_t *session;
197 l2t_main_t *lm = &l2t_main;
202 pool_foreach (session, lm->sessions,
204 session_index = session - lm->sessions;
206 session_index_to_counter_index (session_index,
207 SESSION_COUNTER_USER_TO_NETWORK);
208 vlib_zero_combined_counter (&lm->counter_main, counter_index);
209 vlib_zero_combined_counter (&lm->counter_main, counter_index+1);
213 vlib_cli_output (vm, "Cleared %d active counters\n", nincr);
218 static VLIB_CLI_COMMAND (clear_counters_command) = {
219 .path = "clear counters",
220 .short_help = "clear all active counters",
221 .function = clear_counters_command_fn,
224 static clib_error_t *
225 l2tp_session_add_command_fn (vlib_main_t * vm,
226 unformat_input_t * input,
227 vlib_cli_command_t * cmd)
229 ip6_address_t client_address, our_address;
230 ip6_address_t * dst_address_copy, * src_address_copy;
231 unformat_input_t _line_input, * line_input = &_line_input;
233 u32 sw_if_index = (u32)~0;
234 l2t_main_t *lm = &l2t_main;
237 vnet_hw_interface_t * hi;
238 vnet_sw_interface_t * si;
240 uword vlan_and_sw_if_index_key;
242 u64 local_cookie = (u64)~0, remote_cookie = (u64)~0;
243 u32 local_session_id = 1, remote_session_id = 1;
244 int our_address_set = 0, client_address_set = 0;
245 int l2_sublayer_present = 0;
247 /* Get a line of input. */
248 if (! unformat_user (input, unformat_line_input, line_input))
251 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
252 if (unformat (line_input, "client %U",
253 unformat_ip6_address, &client_address))
254 client_address_set = 1;
255 else if (unformat (line_input, "our %U",
256 unformat_ip6_address, &our_address))
258 else if (unformat (line_input, "vlan %d", &vlan_id))
260 else if (unformat (line_input, "l2-interface %U",
261 unformat_vnet_sw_interface,
262 vnet_get_main(), &sw_if_index))
264 else if (unformat (line_input, "interface %U",
265 unformat_vnet_sw_interface,
266 vnet_get_main(), &sw_if_index))
268 else if (unformat (line_input, "local-cookie %llx", &local_cookie))
270 else if (unformat (line_input, "remote-cookie %llx", &remote_cookie))
272 else if (unformat (line_input, "local-session-id %d",
275 else if (unformat (line_input, "remote-session-id %d",
278 else if (unformat (line_input, "l2-sublayer-present"))
279 l2_sublayer_present = 1;
281 return clib_error_return (0, "parse error: '%U'",
282 format_unformat_error, line_input);
285 unformat_free (line_input);
287 if (sw_if_index == (u32)~0)
288 return clib_error_return (0, "l2-interface not specified");
289 if (our_address_set == 0)
290 return clib_error_return (0, "our address not specified");
291 if (client_address_set == 0)
292 return clib_error_return (0, "client address not specified");
294 remote_session_id = clib_host_to_net_u32 (remote_session_id);
295 local_session_id = clib_host_to_net_u32 (local_session_id);
297 switch (lm->lookup_type) {
298 case L2T_LOOKUP_SRC_ADDRESS:
299 p = hash_get_mem (lm->session_by_src_address, &client_address);
301 return clib_error_return
302 (0, "Session w/ client address %U already exists",
303 format_ip6_address, &client_address);
306 case L2T_LOOKUP_DST_ADDRESS:
307 p = hash_get_mem (lm->session_by_dst_address, &our_address);
309 return clib_error_return
310 (0, "Session w/ our address %U already exists",
311 format_ip6_address, &our_address);
314 case L2T_LOOKUP_SESSION_ID:
315 p = hash_get (lm->session_by_session_id, local_session_id);
317 return clib_error_return
319 "Session w/ local session id %d already exists",
320 clib_net_to_host_u32 (local_session_id));
327 pool_get (lm->sessions, s);
328 memset (s, 0, sizeof (*s));
329 clib_memcpy (&s->our_address, &our_address, sizeof (s->our_address));
330 clib_memcpy (&s->client_address, &client_address, sizeof (s->client_address));
331 s->sw_if_index = sw_if_index;
332 s->vlan_id = clib_host_to_net_u16 (vlan_id);
333 s->local_cookie = clib_host_to_net_u64 (local_cookie);
334 l2tp_session_set_remote_cookie (s, remote_cookie);
335 s->local_session_id = local_session_id;
336 s->remote_session_id = remote_session_id;
337 s->l2_sublayer_present = l2_sublayer_present;
339 hi = vnet_get_sup_hw_interface (lm->vnet_main, sw_if_index);
340 si = vnet_get_sup_sw_interface (lm->vnet_main, sw_if_index);
342 next_index = vlib_node_add_next (vm, l2t_ip6_node.index,
343 hi->output_node_index);
344 s->l2_output_next_index = next_index;
345 s->l2_output_sw_if_index = si->sw_if_index;
347 /* Setup hash table entries */
348 switch (lm->lookup_type) {
349 case L2T_LOOKUP_SRC_ADDRESS:
350 src_address_copy = clib_mem_alloc (sizeof (*src_address_copy));
351 clib_memcpy (src_address_copy, &client_address, sizeof (*src_address_copy));
352 hash_set_mem (lm->session_by_src_address, src_address_copy,
355 case L2T_LOOKUP_DST_ADDRESS:
356 dst_address_copy = clib_mem_alloc (sizeof (*dst_address_copy));
357 clib_memcpy (dst_address_copy, &our_address, sizeof (*dst_address_copy));
358 hash_set_mem (lm->session_by_dst_address, dst_address_copy,
361 case L2T_LOOKUP_SESSION_ID:
362 hash_set (lm->session_by_session_id, local_session_id,
370 vlan_and_sw_if_index_key = ((uword)(s->vlan_id)<<32) | sw_if_index;
371 hash_set (lm->session_by_vlan_and_rx_sw_if_index,
372 vlan_and_sw_if_index_key, s - lm->sessions);
374 /* validate counters */
376 session_index_to_counter_index (s - lm->sessions,
377 SESSION_COUNTER_USER_TO_NETWORK);
378 vlib_validate_counter (&lm->counter_main, counter_index);
379 vlib_validate_counter (&lm->counter_main, counter_index+1);
381 /* Set promiscuous mode on the l2 interface */
382 ethernet_set_flags (lm->vnet_main, hi->hw_if_index,
383 ETHERNET_INTERFACE_FLAG_ACCEPT_ALL);
384 vnet_hw_interface_rx_redirect_to_node (lm->vnet_main, hi->hw_if_index,
389 static VLIB_CLI_COMMAND (l2tp_session_add_command) = {
390 .path = "l2tp session add",
392 "l2tp session add client <ip6> our <ip6> vlan <id> local-cookie <hex> remote-cookie <hex> local-session <dec> remote-session <dec> l2-interface <int>",
393 .function = l2tp_session_add_command_fn,
396 static clib_error_t *
397 l2tp_session_del_command_fn (vlib_main_t * vm,
398 unformat_input_t * input,
399 vlib_cli_command_t * cmd)
401 l2t_main_t *lm = &l2t_main;
406 uword vlan_and_sw_if_index_key;
408 if (!unformat (input, "%d", &session_index))
409 return clib_error_return (0, "missing session index: '%U'",
410 format_unformat_error, input);
412 if (pool_is_free_index (lm->sessions, session_index))
413 return clib_error_return (0, "session %d not in use", session_index);
415 s = pool_elt_at_index (lm->sessions, session_index);
417 switch (lm->lookup_type) {
418 case L2T_LOOKUP_SRC_ADDRESS:
419 hp = hash_get_pair_mem (lm->session_by_src_address, &s->client_address);
421 key = (void *)(hp->key);
422 hash_unset_mem (lm->session_by_src_address, &s->client_address);
425 clib_warning ("session %d src address key %U AWOL",
427 format_ip6_address, &s->client_address);
430 case L2T_LOOKUP_DST_ADDRESS:
431 hp = hash_get_pair_mem (lm->session_by_dst_address, &s->our_address);
433 key = (void *)(hp->key);
434 hash_unset_mem (lm->session_by_dst_address, &s->our_address);
437 clib_warning ("session %d dst address key %U AWOL",
439 format_ip6_address, &s->our_address);
442 case L2T_LOOKUP_SESSION_ID:
443 hash_unset (lm->session_by_session_id, s->local_session_id);
450 vlan_and_sw_if_index_key = ((uword)(s->vlan_id)<<32) | s->sw_if_index;
452 hash_unset (lm->session_by_vlan_and_rx_sw_if_index, vlan_and_sw_if_index_key);
454 pool_put (lm->sessions, s);
458 static VLIB_CLI_COMMAND (l2tp_session_del_command) = {
459 .path = "l2tp session delete",
461 "l2tp session delete <session-id>",
462 .function = l2tp_session_del_command_fn,
465 static clib_error_t *
466 l2tp_session_cookie_command_fn (vlib_main_t * vm,
467 unformat_input_t * input,
468 vlib_cli_command_t * cmd)
470 l2t_main_t *lm = &l2t_main;
473 u64 lcl_ro_cookie = (u64)~0, rem_ro_cookie = (u64)~0;
476 if (!unformat (input, "%d", &session_index))
477 return clib_error_return (0, "missing session index: '%U'",
478 format_unformat_error, input);
480 if (pool_is_free_index (lm->sessions, session_index))
481 return clib_error_return (0, "session %d not in use", session_index);
483 s = pool_elt_at_index (lm->sessions, session_index);
485 if (unformat (input, "commit")) {
486 if (!s->cookie_flags) {
487 return clib_error_return (0,
488 "no rollover cookie ready to commit");
490 l2tp_session_cookie_commit (s);
494 if (!unformat (input, "rollover"))
495 return clib_error_return (0, "missing 'commit|rollover': '%U'",
496 format_unformat_error, input);
497 if (unformat (input, "local %llx", &lcl_ro_cookie)) {
498 cookie_flags |= L2TP_COOKIE_ROLLOVER_LOCAL;
499 l2tp_session_set_local_rollover_cookie (s, lcl_ro_cookie);
501 if (unformat (input, "remote %llx", &rem_ro_cookie)) {
502 cookie_flags |= L2TP_COOKIE_ROLLOVER_REMOTE;
503 l2tp_session_set_remote_cookie (s, rem_ro_cookie);
506 return clib_error_return (0, "no rollover cookie specified");
511 static VLIB_CLI_COMMAND (l2tp_session_cookie_command) = {
512 .path = "l2tp session cookie",
514 "l2tp session cookie <session id> commit|rollover [local <hex>] [remote <hex>]",
515 .function = l2tp_session_cookie_command_fn,