1 ;;; tunnel-c-skel.el - tunnel encap cli / api
5 (define-skeleton skel-tunnel-c
6 "Insert a tunnel cli/api implementation"
8 '(setq encap_stack (skeleton-read "encap_stack (e.g ip4_udp_lisp): "))
9 '(setq ENCAP_STACK (upcase encap_stack))
10 '(setq encap-stack (replace-regexp-in-string "_" "-" encap_stack))
12 #include <vnet/" encap-stack "/" encap_stack" .h>
14 " encap_stack "_main_t " encap_stack "_main;
16 static u8 * format_decap_next (u8 * s, va_list * args)
18 u32 next_index = va_arg (*args, u32);
22 case " ENCAP_STACK "_INPUT_NEXT_DROP:
23 return format (s, \"drop\");
24 case " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT:
25 return format (s, \"ip4\");
26 case " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT:
27 return format (s, \"ip6\");
28 case " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP:
29 return format (s, \"" encap-stack "\");
31 return format (s, \"unknown %d\", next_index);
36 u8 * format_" encap_stack "_tunnel (u8 * s, va_list * args)
38 " encap_stack "_tunnel_t * t = va_arg (*args, " encap_stack "_tunnel_t *);
39 " encap_stack "_main_t * ngm = &" encap_stack "_main;
42 \"[%d] %U (src) %U (dst) fibs: encap %d, decap %d\",
44 format_ip4_address, &t->src,
45 format_ip4_address, &t->dst,
49 s = format (s, \" decap next %U\\n\", format_decap_next, t->decap_next_index);
50 /* FIXME: add protocol details */
54 static u8 * format_" encap_stack "_name (u8 * s, va_list * args)
56 u32 dev_instance = va_arg (*args, u32);
57 return format (s, \"" encap_stack "_tunnel%d\", dev_instance);
60 static uword dummy_interface_tx (vlib_main_t * vm,
61 vlib_node_runtime_t * node,
64 clib_warning (\"you shouldn't be here, leaking buffers...\");
65 return frame->n_vectors;
68 VNET_DEVICE_CLASS (" encap_stack "_device_class,static) = {
69 .name = "" ENCAP_STACK "",
70 .format_device_name = format_" encap_stack "_name,
71 .format_tx_trace = format_" encap_stack "_encap_trace,
72 .tx_function = dummy_interface_tx,
75 static uword dummy_set_rewrite (vnet_main_t * vnm,
80 uword max_rewrite_bytes)
85 u8 * format_" encap_stack "_header_with_length (u8 * s, va_list * args)
87 " encap_stack "_header_t * h = va_arg (*args, " encap_stack "_header_t *);
88 u32 max_header_bytes = va_arg (*args, u32);
91 header_bytes = sizeof (h[0]);
92 if (max_header_bytes != 0 && header_bytes > max_header_bytes)
93 return format (s, \"" encap-stack "header truncated\");
95 /* FIXME: pretty-print an " encap_stack " header */
100 VNET_HW_INTERFACE_CLASS (" encap_stack "_hw_class) = {
101 .name = \"" ENCAP_STACK "\",
102 .format_header = format_" encap_stack "_header_with_length,
103 .set_rewrite = dummy_set_rewrite,
106 #define foreach_copy_field \
111 _(decap_next_index) \
112 _(FIXME_ADD_ALL_COPY_FIELDS )
114 static int " encap_stack "_rewrite (" encap_stack "_tunnel_t * t)
118 " encap_stack "_header_t * h0;
123 vec_validate_aligned (rw, len-1, CLIB_CACHE_LINE_BYTES);
125 h0 = (ip4_udp_" encap_stack "_header_t *) rw;
127 /* FIXME: build the actual header here... */
129 /* Fixed portion of the (outer) ip4 header */
131 ip0->ip_version_and_header_length = 0x45;
133 ip0->protocol = IP_PROTOCOL_UDP;
135 /* we'll fix up the ip4 header length and checksum after-the-fact */
136 ip0->src_address.as_u32 = t->src.as_u32;
137 ip0->dst_address.as_u32 = t->dst.as_u32;
138 ip0->checksum = ip4_header_checksum (ip0);
140 /* UDP header, randomize src port on something, maybe? */
141 h0->udp.src_port = clib_host_to_net_u16 (4341);
142 h0->udp.dst_port = clib_host_to_net_u16 (UDP_DST_PORT_" encap_stack ");
144 /* $$$ build a <mumble> tunnel header here */
150 int vnet_" encap_stack "_add_del_tunnel
151 (vnet_" encap_stack "_add_del_tunnel_args_t *a, u32 * hw_if_indexp)
153 " encap_stack "_main_t * ngm = &" encap_stack "_main;
154 " encap_stack "_tunnel_t *t = 0;
155 vnet_main_t * vnm = ngm->vnet_main;
156 vnet_hw_interface_t * hi;
158 u32 hw_if_index = ~0;
160 " encap_stack "_tunnel_key_t key, *key_copy;
163 key.FIXME = clib_host_to_net_XXX(FIXME);
165 p = hash_get_mem (ngm->" encap_stack "_tunnel_by_key, &key);
169 /* adding a tunnel: tunnel must not already exist */
171 return VNET_API_ERROR_INVALID_VALUE;
173 pool_get_aligned (ngm->tunnels, t, CLIB_CACHE_LINE_BYTES);
174 memset (t, 0, sizeof (*t));
176 /* copy from arg structure */
177 #define _(x) t->x = a->x;
181 rv = " encap_stack "_rewrite (t);
185 pool_put (ngm->tunnels, t);
189 /* $$$$ use a simple hash if you can ... */
190 key_copy = clib_mem_alloc (sizeof (*key_copy));
191 clib_memcpy (key_copy, &key, sizeof (*key_copy));
193 hash_set_mem (ngm->" encap_stack "_tunnel_by_key, key_copy,
197 * interface freelist / recycle shtik
198 * This simple implementation rapidly reuses freed tunnel interfaces.
199 * Consider whether to refcount, etc. etc.
201 if (vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) > 0)
203 hw_if_index = ngm->free_" encap_stack "_tunnel_hw_if_indices
204 [vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices)-1];
205 _vec_len (ngm->free_" encap_stack "_tunnel_hw_if_indices) -= 1;
207 hi = vnet_get_hw_interface (vnm, hw_if_index);
208 hi->dev_instance = t - ngm->tunnels;
209 hi->hw_instance = hi->dev_instance;
213 hw_if_index = vnet_register_interface
214 (vnm, " encap_stack "_device_class.index, t - ngm->tunnels,
215 " encap_stack "_hw_class.index, t - ngm->tunnels);
216 hi = vnet_get_hw_interface (vnm, hw_if_index);
217 hi->output_node_index = " encap_stack "_encap_node.index;
220 t->hw_if_index = hw_if_index;
222 vnet_sw_interface_set_flags (vnm, hi->sw_if_index,
223 VNET_SW_INTERFACE_FLAG_ADMIN_UP);
227 /* deleting a tunnel: tunnel must exist */
229 return VNET_API_ERROR_NO_SUCH_ENTRY;
231 t = pool_elt_at_index (ngm->tunnels, p[0]);
233 vnet_sw_interface_set_flags (vnm, t->hw_if_index, 0 /* down */);
234 vec_add1 (ngm->free_" encap_stack "_tunnel_hw_if_indices, t->hw_if_index);
236 hp = hash_get_pair (ngm->" encap_stack "_tunnel_by_key, &key);
237 key_copy = (void *)(hp->key);
238 hash_unset_mem (ngm->" encap_stack "_tunnel_by_key, &key);
239 clib_mem_free (key_copy);
241 vec_free (t->rewrite);
242 pool_put (ngm->tunnels, t);
246 *hw_if_indexp = hw_if_index;
251 static u32 fib_index_from_fib_id (u32 fib_id)
253 ip4_main_t * im = &ip4_main;
256 p = hash_get (im->fib_index_by_table_id, fib_id);
263 static uword unformat_decap_next (unformat_input_t * input, va_list * args)
265 u32 * result = va_arg (*args, u32 *);
268 if (unformat (input, \"drop\"))
269 *result = " ENCAP_STACK "_INPUT_NEXT_DROP;
270 else if (unformat (input, \"ip4\"))
271 *result = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
272 else if (unformat (input, \"ip6\"))
273 *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT;
274 else if (unformat (input, \"ethernet\"))
275 *result = " ENCAP_STACK "_INPUT_NEXT_IP6_INPUT;
276 else if (unformat (input, \"" encap-stack "\"))
277 *result = " ENCAP_STACK "_INPUT_NEXT_" ENCAP_STACK "_ENCAP;
278 else if (unformat (input, \"%d\", &tmp))
285 static clib_error_t *
286 " encap_stack "_add_del_tunnel_command_fn (vlib_main_t * vm,
287 unformat_input_t * input,
288 vlib_cli_command_t * cmd)
290 unformat_input_t _line_input, * line_input = &_line_input;
291 ip4_address_t src, dst;
295 u32 encap_fib_index = 0;
296 u32 decap_fib_index = 0;
297 u8 next_protocol = " ENCAP_STACK "_NEXT_PROTOCOL_IP4;
298 u32 decap_next_index = " ENCAP_STACK "_INPUT_NEXT_IP4_INPUT;
299 u8 flags = " ENCAP_STACK "_FLAGS_P;
306 vnet_" encap_stack "_add_del_tunnel_args_t _a, * a = &_a;
308 /* Get a line of input. */
309 if (! unformat_user (input, unformat_line_input, line_input))
312 while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) {
313 if (unformat (line_input, \"del\"))
315 else if (unformat (line_input, \"src %U\",
316 unformat_ip4_address, &src))
318 else if (unformat (line_input, \"dst %U\",
319 unformat_ip4_address, &dst))
321 else if (unformat (line_input, \"encap-vrf-id %d\", &tmp))
323 encap_fib_index = fib_index_from_fib_id (tmp);
324 if (encap_fib_index == ~0)
325 return clib_error_return (0, \"nonexistent encap fib id %d\", tmp);
327 else if (unformat (line_input, \"decap-vrf-id %d\", &tmp))
329 decap_fib_index = fib_index_from_fib_id (tmp);
330 if (decap_fib_index == ~0)
331 return clib_error_return (0, \"nonexistent decap fib id %d\", tmp);
333 else if (unformat (line_input, \"decap-next %U\", unformat_decap_next,
336 else if (unformat(line_input, \"next-ip4\"))
338 else if (unformat(line_input, \"next-ip6\"))
340 else if (unformat(line_input, \"next-ethernet\"))
342 else if (unformat(line_input, \"next-nsh\"))
345 * $$$ allow the user to specify anything they want
346 * in the " ENCAP_STACK " header
349 return clib_error_return (0, \"parse error: '%U'\",
350 format_unformat_error, line_input);
353 unformat_free (line_input);
356 return clib_error_return (0, \"tunnel src address not specified\");
359 return clib_error_return (0, \"tunnel dst address not specified\");
361 memset (a, 0, sizeof (*a));
365 #define _(x) a->x = x;
369 rv = vnet_" encap_stack "_add_del_tunnel (a, 0 /* hw_if_indexp */);
375 case VNET_API_ERROR_INVALID_VALUE:
376 return clib_error_return (0, \"tunnel already exists...\");
378 case VNET_API_ERROR_NO_SUCH_ENTRY:
379 return clib_error_return (0, \"tunnel does not exist...\");
382 return clib_error_return
383 (0, \"vnet_" encap_stack "_add_del_tunnel returned %d\", rv);
389 VLIB_CLI_COMMAND (create_" encap_stack "_tunnel_command, static) = {
390 .path = \"lisp gpe tunnel\",
392 \"<mumble> tunnel src <ip4-addr> dst <ip4-addr>\\n\"
393 \" [encap-fib-id <nn>] [decap-fib-id <nn>]\\n\"
394 \" [decap-next [ip4|ip6|ethernet|nsh-encap|<nn>]][del]\\n\",
395 .function = " encap_stack "_add_del_tunnel_command_fn,
398 static clib_error_t *
399 show_" encap_stack "_tunnel_command_fn (vlib_main_t * vm,
400 unformat_input_t * input,
401 vlib_cli_command_t * cmd)
403 " encap_stack "_main_t * ngm = &" encap_stack "_main;
404 " encap_stack "_tunnel_t * t;
406 if (pool_elts (ngm->tunnels) == 0)
407 vlib_cli_output (vm, \"No lisp-gpe tunnels configured...\");
409 pool_foreach (t, ngm->tunnels,
411 vlib_cli_output (vm, \"%U\", format_" encap_stack "_tunnel);
417 VLIB_CLI_COMMAND (show_" encap_stack "_tunnel_command, static) = {
418 .path = \"show lisp gpe tunnel\",
419 .function = show_" encap_stack "_tunnel_command_fn,
422 clib_error_t *" encap_stack "_init (vlib_main_t *vm)
424 " encap_stack "_main_t *ngm = &" encap_stack "_main;
426 ngm->vnet_main = vnet_get_main();
429 ngm->" encap_stack "_tunnel_by_key
430 = hash_create_mem (0, sizeof(" encap_stack "_tunnel_key_t), sizeof (uword));
432 /* YMMV, register with the local netstack */
433 udp_register_dst_port (vm, UDP_DST_PORT_" encap_stack ",
434 " encap_stack "_input_node.index, 1 /* is_ip4 */);
438 VLIB_INIT_FUNCTION(" encap_stack "_init);