FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
dhcp_client_detect.c
Go to the documentation of this file.
1 /*
2  * DHCP feature; applied as an input feature to select DHCP packets
3  *
4  * Copyright (c) 2013 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vnet/dhcp/client.h>
19 #include <vnet/udp/udp.h>
20 
21 #define foreach_dhcp_client_detect \
22  _(EXTRACT, "Extract")
23 
24 typedef enum
25 {
26 #define _(sym,str) DHCP_CLIENT_DETECT_ERROR_##sym,
28 #undef _
31 
33 #define _(sym,string) string,
35 #undef _
36 };
37 
38 typedef enum
39 {
40 #define _(sym,str) DHCP_CLIENT_DETECT_NEXT_##sym,
42 #undef _
45 
46 /**
47  * per-packet trace data
48  */
50 {
51  /* per-pkt trace data */
54 
55 static uword
57  vlib_node_runtime_t * node, vlib_frame_t * frame)
58 {
59  dhcp_client_detect_next_t next_index;
60  u16 dhcp_client_port_network_order;
61  u32 n_left_from, *from, *to_next;
62  u32 extractions;
63 
64  dhcp_client_port_network_order =
65  clib_net_to_host_u16 (UDP_DST_PORT_dhcp_to_client);
66  next_index = 0;
67  extractions = 0;
68  n_left_from = frame->n_vectors;
69  from = vlib_frame_vector_args (frame);
70 
71  while (n_left_from > 0)
72  {
73  u32 n_left_to_next;
74 
75  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
76 
77  /*
78  * This loop is optimised not so we can really quickly process DHCp
79  * offers... but so we can quickly sift them out when the interface
80  * is also receving 'normal' packets
81  */
82  while (n_left_from >= 8 && n_left_to_next >= 4)
83  {
84  udp_header_t *udp0, *udp1, *udp2, *udp3;
85  ip4_header_t *ip0, *ip1, *ip2, *ip3;
86  vlib_buffer_t *b0, *b1, *b2, *b3;
87  u32 next0, next1, next2, next3;
88  u32 bi0, bi1, bi2, bi3;
89 
90  next0 = next1 = next2 = next3 = ~0;
91  bi0 = to_next[0] = from[0];
92  bi1 = to_next[1] = from[1];
93  bi2 = to_next[2] = from[2];
94  bi3 = to_next[3] = from[3];
95 
96  /* Prefetch next iteration. */
97  {
98  vlib_buffer_t *p2, *p3, *p4, *p5;
99 
100  p2 = vlib_get_buffer (vm, from[2]);
101  p3 = vlib_get_buffer (vm, from[3]);
102  p4 = vlib_get_buffer (vm, from[4]);
103  p5 = vlib_get_buffer (vm, from[5]);
104 
105  vlib_prefetch_buffer_header (p2, STORE);
106  vlib_prefetch_buffer_header (p3, STORE);
107  vlib_prefetch_buffer_header (p4, STORE);
108  vlib_prefetch_buffer_header (p5, STORE);
109 
110  CLIB_PREFETCH (p2->data, sizeof (ip0[0]) + sizeof (udp0[0]),
111  STORE);
112  CLIB_PREFETCH (p3->data, sizeof (ip0[0]) + sizeof (udp0[0]),
113  STORE);
114  CLIB_PREFETCH (p4->data, sizeof (ip0[0]) + sizeof (udp0[0]),
115  STORE);
116  CLIB_PREFETCH (p5->data, sizeof (ip0[0]) + sizeof (udp0[0]),
117  STORE);
118  }
119 
120  from += 4;
121  to_next += 4;
122  n_left_from -= 4;
123  n_left_to_next -= 4;
124 
125  b0 = vlib_get_buffer (vm, bi0);
126  b1 = vlib_get_buffer (vm, bi1);
127  b2 = vlib_get_buffer (vm, bi2);
128  b3 = vlib_get_buffer (vm, bi3);
129  ip0 = vlib_buffer_get_current (b0);
130  ip1 = vlib_buffer_get_current (b1);
131  ip2 = vlib_buffer_get_current (b2);
132  ip3 = vlib_buffer_get_current (b2);
133 
134  vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
135  &next0, b0);
136  vnet_feature_next (vnet_buffer (b1)->sw_if_index[VLIB_TX],
137  &next1, b1);
138  vnet_feature_next (vnet_buffer (b2)->sw_if_index[VLIB_TX],
139  &next2, b2);
140  vnet_feature_next (vnet_buffer (b3)->sw_if_index[VLIB_TX],
141  &next3, b3);
142 
143  if (ip0->protocol == IP_PROTOCOL_UDP)
144  {
145  udp0 = (udp_header_t *) (ip0 + 1);
146 
147  if (dhcp_client_port_network_order == udp0->dst_port)
148  {
149  next0 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
150  extractions++;
151  }
152  }
153  if (ip1->protocol == IP_PROTOCOL_UDP)
154  {
155  udp1 = (udp_header_t *) (ip1 + 1);
156 
157  if (dhcp_client_port_network_order == udp1->dst_port)
158  {
159  next1 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
160  extractions++;
161  }
162  }
163  if (ip2->protocol == IP_PROTOCOL_UDP)
164  {
165  udp2 = (udp_header_t *) (ip2 + 1);
166 
167  if (dhcp_client_port_network_order == udp2->dst_port)
168  {
169  next2 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
170  extractions++;
171  }
172  }
173  if (ip3->protocol == IP_PROTOCOL_UDP)
174  {
175  udp3 = (udp_header_t *) (ip3 + 1);
176 
177  if (dhcp_client_port_network_order == udp3->dst_port)
178  {
179  next3 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
180  extractions++;
181  }
182  }
183 
184  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
185  {
187  vlib_add_trace (vm, node, b0, sizeof (*t));
188  t->extracted = (next0 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
189  }
190  if (PREDICT_FALSE (b1->flags & VLIB_BUFFER_IS_TRACED))
191  {
193  vlib_add_trace (vm, node, b1, sizeof (*t));
194  t->extracted = (next1 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
195  }
196  if (PREDICT_FALSE (b2->flags & VLIB_BUFFER_IS_TRACED))
197  {
199  vlib_add_trace (vm, node, b2, sizeof (*t));
200  t->extracted = (next2 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
201  }
202  if (PREDICT_FALSE (b3->flags & VLIB_BUFFER_IS_TRACED))
203  {
205  vlib_add_trace (vm, node, b3, sizeof (*t));
206  t->extracted = (next3 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
207  }
208 
209  /* verify speculative enqueue, maybe switch current next frame */
210  vlib_validate_buffer_enqueue_x4 (vm, node, next_index,
211  to_next, n_left_to_next,
212  bi0, bi1, bi2, bi3,
213  next0, next1, next2, next3);
214  }
215 
216  while (n_left_from > 0 && n_left_to_next > 0)
217  {
218  udp_header_t *udp0;
219  vlib_buffer_t *b0;
220  ip4_header_t *ip0;
221  u32 next0 = ~0;
222  u32 bi0;
223 
224  bi0 = from[0];
225  to_next[0] = bi0;
226  from += 1;
227  to_next += 1;
228  n_left_from -= 1;
229  n_left_to_next -= 1;
230 
231  b0 = vlib_get_buffer (vm, bi0);
232  ip0 = vlib_buffer_get_current (b0);
233 
234  /*
235  * when this feature is applied on an interface that is already
236  * accepting packets (because e.g. the interface has other addresses
237  * assigned) we are looking for the preverbial needle in the haystack
238  * so assume the packet is not the one we are looking for.
239  */
240  vnet_feature_next (vnet_buffer (b0)->sw_if_index[VLIB_TX],
241  &next0, b0);
242 
243  /*
244  * all we are looking for here is DHCP/BOOTP packet-to-client
245  * UDO port.
246  */
247  if (ip0->protocol == IP_PROTOCOL_UDP)
248  {
249  udp0 = (udp_header_t *) (ip0 + 1);
250 
251  if (dhcp_client_port_network_order == udp0->dst_port)
252  {
253  next0 = DHCP_CLIENT_DETECT_NEXT_EXTRACT;
254  extractions++;
255  }
256  }
257 
258  if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
259  {
261  vlib_add_trace (vm, node, b0, sizeof (*t));
262  t->extracted = (next0 == DHCP_CLIENT_DETECT_NEXT_EXTRACT);
263  }
264 
265  /* verify speculative enqueue, maybe switch current next frame */
266  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
267  to_next, n_left_to_next,
268  bi0, next0);
269  }
270 
271  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
272  }
273 
275  DHCP_CLIENT_DETECT_ERROR_EXTRACT, extractions);
276 
277  return frame->n_vectors;
278 }
279 
280 /* packet trace format function */
281 static u8 *
282 format_dhcp_client_detect_trace (u8 * s, va_list * args)
283 {
284  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
285  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
287  va_arg (*args, dhcp_client_detect_trace_t *);
288 
289  s = format (s, "dhcp-client-detect: %s", (t->extracted ? "yes" : "no"));
290 
291  return s;
292 }
293 
294 /* *INDENT-OFF* */
296  .function = dhcp_client_detect_node_fn,
297  .name = "ip4-dhcp-client-detect",
298  .vector_size = sizeof (u32),
299  .format_trace = format_dhcp_client_detect_trace,
300  .type = VLIB_NODE_TYPE_INTERNAL,
301 
303  .error_strings = dhcp_client_detect_error_strings,
304 
305  .n_next_nodes = DHCP_CLIENT_DETECT_N_NEXT,
306  .next_nodes = {
307  /*
308  * Jump straight to the UDP dispatch node thus avoiding
309  * the RPF checks in ip4-local that will fail
310  */
311  [DHCP_CLIENT_DETECT_NEXT_EXTRACT] = "ip4-udp-lookup",
312  },
313 };
314 
317 
318 VNET_FEATURE_INIT (ip4_dvr_reinject_feat_node, static) =
319 {
320  .arc_name = "ip4-unicast",
321  .node_name = "ip4-dhcp-client-detect",
322  .runs_before = VNET_FEATURES ("ip4-not-enabled"),
323 };
324 
325 /* *INDENT-ON* */
326 
327 /*
328  * fd.io coding-style-patch-verification: ON
329  *
330  * Local Variables:
331  * eval: (c-set-style "gnu")
332  * End:
333  */
VLIB_NODE_FUNCTION_MULTIARCH(dhcp_client_detect_node, dhcp_client_detect_node_fn)
#define CLIB_UNUSED(x)
Definition: clib.h:79
per-packet trace data
struct dhcp_client_detect_trace_t_ dhcp_client_detect_trace_t
per-packet trace data
#define vlib_validate_buffer_enqueue_x4(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, bi2, bi3, next0, next1, next2, next3)
Finish enqueueing four buffers forward in the graph.
Definition: buffer_node.h:138
static uword dhcp_client_detect_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:191
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:209
dhcp_client_detect_next_t
#define PREDICT_FALSE(x)
Definition: clib.h:105
static u8 * format_dhcp_client_detect_trace(u8 *s, va_list *args)
u32 node_index
Node index.
Definition: node.h:437
static_always_inline void vnet_feature_next(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:221
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1166
#define foreach_dhcp_client_detect
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u16 n_vectors
Definition: node.h:344
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:74
vlib_main_t * vm
Definition: buffer.c:294
#define ARRAY_LEN(x)
Definition: clib.h:59
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
unsigned int u32
Definition: types.h:88
#define VNET_FEATURES(...)
Definition: feature.h:375
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
vlib_node_registration_t dhcp_client_detect_node
(constructor) VLIB_REGISTER_NODE (dhcp_client_detect_node)
Definition: defs.h:47
unsigned short u16
Definition: types.h:57
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
dhcp_client_detect_error_t
#define vnet_buffer(b)
Definition: buffer.h:372
u8 data[0]
Packet data.
Definition: buffer.h:179
static char * dhcp_client_detect_error_strings[]
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:111
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
VNET_FEATURE_INIT(ip4_dvr_reinject_feat_node, static)