VPP-491: Update CLI Command documentation for "show ip fib" and "show ip6 fib".
[vpp.git] / vnet / vnet / ip / punt.c
1 /*
2  * Copyright (c) 2016 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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /**
17  * @file
18  * @brief Local TCP/IP stack punt infrastructure.
19  *
20  * Provides a set of VPP nodes togather with the relevant APIs and CLI
21  * commands in order to adjust and dispatch packets from the VPP data plane
22  * to the local TCP/IP stack
23  */
24 #include <vlib/vlib.h>
25 #include <vnet/pg/pg.h>
26 #include <vnet/ip/udp.h>
27 #include <vnet/ip/punt.h>
28
29 #define foreach_punt_next \
30   _ (PUNT, "error-punt")
31
32 typedef enum
33 {
34 #define _(s,n) PUNT_NEXT_##s,
35   foreach_punt_next
36 #undef _
37     PUNT_N_NEXT,
38 } punt_next_t;
39
40 vlib_node_registration_t udp4_punt_node;
41 vlib_node_registration_t udp6_punt_node;
42
43 /** @brief IPv4/IPv6 UDP punt node main loop.
44
45     This is the main loop inline function for IPv4/IPv6 UDP punt
46     transition node.
47
48     @param vm vlib_main_t corresponding to the current thread
49     @param node vlib_node_runtime_t
50     @param frame vlib_frame_t whose contents should be dispatched
51     @param is_ipv4 indicates if called for IPv4 or IPv6 node
52 */
53 always_inline uword
54 udp46_punt_inline (vlib_main_t * vm,
55                    vlib_node_runtime_t * node,
56                    vlib_frame_t * from_frame, int is_ip4)
57 {
58   u32 n_left_from, *from, *to_next;
59   word advance;
60
61   from = vlib_frame_vector_args (from_frame);
62   n_left_from = from_frame->n_vectors;
63
64   /* udp[46]_lookup hands us the data payload, not the IP header */
65   if (is_ip4)
66     advance = -(sizeof (ip4_header_t) + sizeof (udp_header_t));
67   else
68     advance = -(sizeof (ip6_header_t) + sizeof (udp_header_t));
69
70   while (n_left_from > 0)
71     {
72       u32 n_left_to_next;
73
74       vlib_get_next_frame (vm, node, PUNT_NEXT_PUNT, to_next, n_left_to_next);
75
76       while (n_left_from > 0 && n_left_to_next > 0)
77         {
78           u32 bi0;
79           vlib_buffer_t *b0;
80
81           bi0 = from[0];
82           to_next[0] = bi0;
83           from += 1;
84           to_next += 1;
85           n_left_from -= 1;
86           n_left_to_next -= 1;
87
88           b0 = vlib_get_buffer (vm, bi0);
89           vlib_buffer_advance (b0, advance);
90           b0->error = node->errors[PUNT_ERROR_UDP_PORT];
91         }
92
93       vlib_put_next_frame (vm, node, PUNT_NEXT_PUNT, n_left_to_next);
94     }
95
96   return from_frame->n_vectors;
97 }
98
99 static char *punt_error_strings[] = {
100 #define punt_error(n,s) s,
101 #include "punt_error.def"
102 #undef punt_error
103 };
104
105 /** @brief IPv4 UDP punt node.
106     @node ip4-udp-punt
107
108     This is the IPv4 UDP punt transition node. It is registered as a next
109     node for the "ip4-udp-lookup" handling UDP port(s) requested for punt.
110     The buffer's current data pointer is adjusted to the original packet
111     IPv4 header. All buffers are dispatched to "error-punt".
112
113     @param vm vlib_main_t corresponding to the current thread
114     @param node vlib_node_runtime_t
115     @param frame vlib_frame_t whose contents should be dispatched
116
117     @par Graph mechanics: next index usage
118
119     @em Sets:
120     - <code>vnet_buffer(b)->current_data</code>
121     - <code>vnet_buffer(b)->current_len</code>
122
123     <em>Next Index:</em>
124     - Dispatches the packet to the "error-punt" node
125 */
126 static uword
127 udp4_punt (vlib_main_t * vm,
128            vlib_node_runtime_t * node, vlib_frame_t * from_frame)
129 {
130   return udp46_punt_inline (vm, node, from_frame, 1 /* is_ip4 */ );
131 }
132
133 /** @brief IPv6 UDP punt node.
134     @node ip6-udp-punt
135
136     This is the IPv6 UDP punt transition node. It is registered as a next
137     node for the "ip6-udp-lookup" handling UDP port(s) requested for punt.
138     The buffer's current data pointer is adjusted to the original packet
139     IPv6 header. All buffers are dispatched to "error-punt".
140
141     @param vm vlib_main_t corresponding to the current thread
142     @param node vlib_node_runtime_t
143     @param frame vlib_frame_t whose contents should be dispatched
144
145     @par Graph mechanics: next index usage
146
147     @em Sets:
148     - <code>vnet_buffer(b)->current_data</code>
149     - <code>vnet_buffer(b)->current_len</code>
150
151     <em>Next Index:</em>
152     - Dispatches the packet to the "error-punt" node
153 */
154 static uword
155 udp6_punt (vlib_main_t * vm,
156            vlib_node_runtime_t * node, vlib_frame_t * from_frame)
157 {
158   return udp46_punt_inline (vm, node, from_frame, 0 /* is_ip4 */ );
159 }
160
161 /* *INDENT-OFF* */
162 VLIB_REGISTER_NODE (udp4_punt_node) = {
163   .function = udp4_punt,
164   .name = "ip4-udp-punt",
165   /* Takes a vector of packets. */
166   .vector_size = sizeof (u32),
167
168   .n_errors = PUNT_N_ERROR,
169   .error_strings = punt_error_strings,
170
171   .n_next_nodes = PUNT_N_NEXT,
172   .next_nodes = {
173 #define _(s,n) [PUNT_NEXT_##s] = n,
174      foreach_punt_next
175 #undef _
176   },
177 };
178
179 VLIB_NODE_FUNCTION_MULTIARCH (udp4_punt_node, udp4_punt)
180
181 VLIB_REGISTER_NODE (udp6_punt_node) = {
182   .function = udp6_punt,
183   .name = "ip6-udp-punt",
184   /* Takes a vector of packets. */
185   .vector_size = sizeof (u32),
186
187   .n_errors = PUNT_N_ERROR,
188   .error_strings = punt_error_strings,
189
190   .n_next_nodes = PUNT_N_NEXT,
191   .next_nodes = {
192 #define _(s,n) [PUNT_NEXT_##s] = n,
193      foreach_punt_next
194 #undef _
195   },
196 };
197
198 VLIB_NODE_FUNCTION_MULTIARCH (udp6_punt_node, udp6_punt)
199 /* *INDENT-ON* */
200
201 /**
202  * @brief Request IP traffic punt to the local TCP/IP stack.
203  *
204  * @em Note
205  * - UDP is the only protocol supported in the current implementation
206  * - When requesting UDP punt port number(s) must be specified
207  * - All TCP traffic is currently punted to the host by default
208  *
209  * @param vm       vlib_main_t corresponding to the current thread
210  * @param ipv      IP protcol version.
211  *                 4 - IPv4, 6 - IPv6, ~0 for both IPv6 and IPv4
212  * @param protocol 8-bits L4 protocol value
213  *                 Only value of 17 (UDP) is currently supported
214  * @param port     16-bits L4 (TCP/IP) port number when applicable
215  *
216  * @returns 0 on success, non-zero value otherwise
217  */
218 clib_error_t *
219 vnet_punt_add_del (vlib_main_t * vm, u8 ipv, u8 protocol, u16 port,
220                    int is_add)
221 {
222   /* For now we only support adding specific UDP port punt */
223   {
224     if (!is_add)
225       return clib_error_return (0, "punt delete is not supported yet");
226
227     if (protocol != IP_PROTOCOL_UDP)
228       return clib_error_return (0,
229                                 "only UDP protocol (%d) is supported, got %d",
230                                 IP_PROTOCOL_UDP, protocol);
231
232     if (port == (u16) ~ 0)
233       return clib_error_return (0, "TCP/UDP port must be specified");
234   }
235
236   if (ipv != (u8) ~ 0)
237     {
238       if (ipv == 4)
239         udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
240       else if (ipv == 6)
241         udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
242       else
243         return clib_error_return (0, "IP version must be 4 or 6, got %d",
244                                   ipv);
245     }
246   else
247     {
248       udp_register_dst_port (vm, port, udp4_punt_node.index, 1);
249       udp_register_dst_port (vm, port, udp6_punt_node.index, 0);
250     }
251
252   return 0;
253
254 }
255
256 static clib_error_t *
257 udp_punt_cli (vlib_main_t * vm,
258               unformat_input_t * input, vlib_cli_command_t * cmd)
259 {
260   u32 udp_port;
261   int is_add = 1;
262   clib_error_t *error;
263
264   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
265     {
266       if (unformat (input, "del"))
267         is_add = 0;
268       else if (unformat (input, "%d", &udp_port))
269         {
270           /* punt both IPv6 and IPv4 when used in CLI */
271           error = vnet_punt_add_del (vm, ~0, IP_PROTOCOL_UDP,
272                                      udp_port, is_add);
273           if (error)
274             clib_error_report (error);
275         }
276     }
277
278   return 0;
279 }
280
281 /*?
282  * The set of '<em>set punt</em>' commands allows specific IP traffic to
283  * be punted to the host TCP/IP stack
284  *
285  * @em Note
286  * - UDP is the only protocol supported in the current implementation
287  * - When requesting UDP punt port number(s) must be specified
288  * - All TCP traffic is currently punted to the host by default
289  *
290  * @cliexpar
291  * @parblock
292  * Example of how to request NTP traffic to be punted
293  * @cliexcmd{set punt udp 125}
294  *
295  * @endparblock
296 ?*/
297 /* *INDENT-OFF* */
298 VLIB_CLI_COMMAND (punt_udp_command, static) = {
299   .path = "set punt udp",
300   .short_help = "set punt udp [del] port-num1 [port-num2 ...]",
301   .function = udp_punt_cli,
302 };
303 /* *INDENT-ON* */
304
305 /*
306  * fd.io coding-style-patch-verification: ON
307  *
308  * Local Variables:
309  * eval: (c-set-style "gnu")
310  * End:
311  */