FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
mss_clamp_node.c
Go to the documentation of this file.
1 /*
2  * mss_clamp_node.c - Node implementing TCP MSS clamping
3  *
4  * Copyright (c) 2018 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 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/pg/pg.h>
20 #include <vppinfra/error.h>
21 #include <mss_clamp/mss_clamp.h>
22 #include <mss_clamp/mss_clamp.api_enum.h>
23 #include <vnet/fib/fib_types.h>
24 #include <vnet/feature/feature.h>
25 #include <vnet/ip/ip4.h>
26 #include <vnet/ip/ip6.h>
27 
30 
31 typedef struct mssc_trace_t_
32 {
35 } mssc_trace_t;
36 
37 /* packet trace format function */
38 static u8 *
39 format_mssc_trace (u8 *s, va_list *args)
40 {
41  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
42  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
43  mssc_trace_t *t = va_arg (*args, mssc_trace_t *);
44 
45  s = format (s, "max mss: %d clamped: %d", t->max_mss, t->clamped);
46  return s;
47 }
48 
49 typedef enum
50 {
53 } mssc_next_t;
54 
55 /*
56  * fixup the maximum segment size if it's a syn packet
57  * return 1 if the mss was changed otherwise 0
58  */
61 {
62  ip_csum_t sum0;
63 
64  if (PREDICT_FALSE (tcp_syn (tcp0)))
65  {
66  u8 opt_len, opts_len, kind;
67  const u8 *data;
68  u16 mss0, new_mss0;
69 
70  opts_len = (tcp_doff (tcp0) << 2) - sizeof (tcp_header_t);
71  data = (const u8 *) (tcp0 + 1);
72 
73  for (; opts_len > 0; opts_len -= opt_len, data += opt_len)
74  {
75  kind = data[0];
76 
77  /* Get options length */
78  if (kind == TCP_OPTION_EOL)
79  break;
80  else if (kind == TCP_OPTION_NOOP)
81  {
82  opt_len = 1;
83  continue;
84  }
85  else
86  {
87  /* broken options */
88  if (opts_len < 2)
89  return 0;
90  opt_len = data[1];
91 
92  /* weird option length */
93  if (opt_len < 2 || opt_len > opts_len)
94  return 0;
95  }
96 
97  if (kind == TCP_OPTION_MSS)
98  {
99  mss0 = *(u16 *) (data + 2);
100  if (clib_net_to_host_u16 (mss0) > max_mss0)
101  {
102  new_mss0 = clib_host_to_net_u16 (max_mss0);
103  *((u16 *) (data + 2)) = new_mss0;
104  sum0 = tcp0->checksum;
105  sum0 = ip_csum_update (sum0, mss0, new_mss0, tcp_header_t,
106  checksum);
107  tcp0->checksum = ip_csum_fold (sum0);
108  return 1;
109  }
110  }
111  }
112  }
113 
114  return 0;
115 }
116 
119  vlib_dir_t dir, fib_protocol_t fproto)
120 {
124  u32 n_left, *from;
125  u32 pkts_clamped = 0;
126 
128  n_left = frame->n_vectors;
129  b = bufs;
130  next = nexts;
131 
133 
134  while (n_left >= 4)
135  {
136  u32 sw_if_index0, sw_if_index1;
137  const u8 *h0, *h1;
138  u32 clamped0, clamped1;
139 
140  /* Prefetch next iteration. */
141  {
142  vlib_prefetch_buffer_header (b[2], LOAD);
143  vlib_prefetch_buffer_header (b[3], LOAD);
144  vlib_prefetch_buffer_data (b[2], LOAD);
145  vlib_prefetch_buffer_data (b[3], LOAD);
146  }
147 
148  sw_if_index0 = vnet_buffer (b[0])->sw_if_index[dir];
149  sw_if_index1 = vnet_buffer (b[1])->sw_if_index[dir];
150  clamped0 = clamped1 = 0;
151 
152  /* speculatively enqueue b0 to the current next frame */
153  vnet_feature_next_u16 (&next[0], b[0]);
154  vnet_feature_next_u16 (&next[1], b[1]);
155 
156  h0 = (u8 *) vlib_buffer_get_current (b[0]);
157  h1 = (u8 *) vlib_buffer_get_current (b[1]);
158  if (VLIB_TX == dir)
159  {
160  h0 += vnet_buffer (b[0])->ip.save_rewrite_length;
161  h1 += vnet_buffer (b[1])->ip.save_rewrite_length;
162  }
163 
164  if (FIB_PROTOCOL_IP4 == fproto)
165  {
166  ip4_header_t *ip0 = (ip4_header_t *) h0;
167  ip4_header_t *ip1 = (ip4_header_t *) h1;
168 
169  if (IP_PROTOCOL_TCP == ip0->protocol)
170  {
171  clamped0 = mssc_mss_fixup (b[0], ip4_next_header (ip0),
172  cm->max_mss4[sw_if_index0]);
173  }
174  if (IP_PROTOCOL_TCP == ip1->protocol)
175  {
176  clamped1 = mssc_mss_fixup (b[1], ip4_next_header (ip1),
177  cm->max_mss4[sw_if_index1]);
178  }
179  }
180  else if (FIB_PROTOCOL_IP6 == fproto)
181  {
182  ip6_header_t *ip0 = (ip6_header_t *) h0;
183  ip6_header_t *ip1 = (ip6_header_t *) h1;
184 
185  if (IP_PROTOCOL_TCP == ip0->protocol)
186  {
187  clamped0 = mssc_mss_fixup (b[0], ip6_next_header (ip0),
188  cm->max_mss6[sw_if_index0]);
189  }
190  if (IP_PROTOCOL_TCP == ip1->protocol)
191  {
192  clamped1 = mssc_mss_fixup (b[1], ip6_next_header (ip1),
193  cm->max_mss6[sw_if_index1]);
194  }
195  }
196 
197  pkts_clamped += clamped0 + clamped1;
198 
199  if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
200  {
201  if (b[0]->flags & VLIB_BUFFER_IS_TRACED)
202  {
203  mssc_trace_t *t;
204 
205  t = vlib_add_trace (vm, node, b[0], sizeof (*t));
206  t->max_mss = (FIB_PROTOCOL_IP4 == fproto) ?
207  cm->max_mss4[sw_if_index0] :
208  cm->max_mss6[sw_if_index0];
209  t->clamped = clamped0;
210  }
211  if (b[1]->flags & VLIB_BUFFER_IS_TRACED)
212  {
213  mssc_trace_t *t;
214 
215  t = vlib_add_trace (vm, node, b[1], sizeof (*t));
216  t->max_mss = (FIB_PROTOCOL_IP4 == fproto) ?
217  cm->max_mss4[sw_if_index1] :
218  cm->max_mss6[sw_if_index1];
219  t->clamped = clamped1;
220  }
221  }
222 
223  b += 2;
224  next += 2;
225  n_left -= 2;
226  }
227 
228  while (n_left > 0)
229  {
230  u32 sw_if_index0;
231  const u8 *h0;
232  u32 clamped0;
233 
234  sw_if_index0 = vnet_buffer (b[0])->sw_if_index[dir];
235  clamped0 = 0;
236 
237  /* speculatively enqueue b0 to the current next frame */
238  vnet_feature_next_u16 (&next[0], b[0]);
239 
240  h0 = (u8 *) vlib_buffer_get_current (b[0]);
241  if (VLIB_TX == dir)
242  h0 += vnet_buffer (b[0])->ip.save_rewrite_length;
243 
244  if (FIB_PROTOCOL_IP4 == fproto)
245  {
246  ip4_header_t *ip0 = (ip4_header_t *) h0;
247 
248  if (IP_PROTOCOL_TCP == ip0->protocol)
249  {
250  clamped0 = mssc_mss_fixup (b[0], ip4_next_header (ip0),
251  cm->max_mss4[sw_if_index0]);
252  }
253  }
254  else if (FIB_PROTOCOL_IP6 == fproto)
255  {
256  ip6_header_t *ip0 = (ip6_header_t *) h0;
257 
258  if (IP_PROTOCOL_TCP == ip0->protocol)
259  {
260  clamped0 = mssc_mss_fixup (b[0], ip6_next_header (ip0),
261  cm->max_mss6[sw_if_index0]);
262  }
263  }
264 
265  pkts_clamped += clamped0;
266 
267  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE) &&
268  (b[0]->flags & VLIB_BUFFER_IS_TRACED)))
269  {
270  mssc_trace_t *t;
271 
272  t = vlib_add_trace (vm, node, b[0], sizeof (*t));
273  t->max_mss = (FIB_PROTOCOL_IP4 == fproto) ?
274  cm->max_mss4[sw_if_index0] :
275  cm->max_mss6[sw_if_index0];
276  t->clamped = clamped0;
277  }
278 
279  b++;
280  next++;
281  n_left--;
282  }
283 
285  vlib_node_increment_counter (vm, node->node_index, MSS_CLAMP_ERROR_CLAMPED,
286  pkts_clamped);
287 
288  return frame->n_vectors;
289 }
290 
291 static uword
293 {
295 }
296 
297 static uword
299 {
301 }
302 
303 static uword
305 {
307 }
308 
309 static uword
311 {
313 }
314 
316 {
317  .function = mssc_ip4_in,
318  .name = "tcp-mss-clamping-ip4-in",
319  .vector_size = sizeof (u32),
320  .format_trace = format_mssc_trace,
322 
323  .n_errors = MSS_CLAMP_N_ERROR,
324  .error_counters = mss_clamp_error_counters,
325 
326  .n_next_nodes = MSSC_N_NEXT,
327  .next_nodes = {
328  [MSSC_NEXT_DROP] = "error-drop",
329  },
330 };
331 
333 {
334  .function = mssc_ip4_out,
335  .name = "tcp-mss-clamping-ip4-out",
336  .vector_size = sizeof (u32),
337  .format_trace = format_mssc_trace,
339 
340  .n_errors = MSS_CLAMP_N_ERROR,
341  .error_counters = mss_clamp_error_counters,
342 
343  .n_next_nodes = MSSC_N_NEXT,
344  .next_nodes = {
345  [MSSC_NEXT_DROP] = "error-drop",
346  },
347 };
348 
350 {
351  .function = mssc_ip6_in,
352  .name = "tcp-mss-clamping-ip6-in",
353  .vector_size = sizeof (u32),
354  .format_trace = format_mssc_trace,
356 
357  .n_errors = MSS_CLAMP_N_ERROR,
358  .error_counters = mss_clamp_error_counters,
359 
360  .n_next_nodes = MSSC_N_NEXT,
361  .next_nodes = {
362  [MSSC_NEXT_DROP] = "error-drop",
363  },
364 };
365 
367 {
368  .function = mssc_ip6_out,
369  .name = "tcp-mss-clamping-ip6-out",
370  .vector_size = sizeof (u32),
371  .format_trace = format_mssc_trace,
373 
374  .n_errors = MSS_CLAMP_N_ERROR,
375  .error_counters = mss_clamp_error_counters,
376 
377  .n_next_nodes = MSSC_N_NEXT,
378  .next_nodes = {
379  [MSSC_NEXT_DROP] = "error-drop",
380  },
381 };
382 
383 VNET_FEATURE_INIT (mssc_ip4_in_feat, static) = {
384  .arc_name = "ip4-unicast",
385  .node_name = "tcp-mss-clamping-ip4-in",
386  .runs_after = VNET_FEATURES ("ip4-policer-classify"),
387 };
388 
389 VNET_FEATURE_INIT (mssc_ip4_out_feat, static) = {
390  .arc_name = "ip4-output",
391  .node_name = "tcp-mss-clamping-ip4-out",
392 };
393 
394 VNET_FEATURE_INIT (mssc_ip6_in_feat, static) = {
395  .arc_name = "ip6-unicast",
396  .node_name = "tcp-mss-clamping-ip6-in",
397  .runs_after = VNET_FEATURES ("ip6-policer-classify"),
398 };
399 
400 VNET_FEATURE_INIT (mssc_ip6_out_feat, static) = {
401  .arc_name = "ip6-output",
402  .node_name = "tcp-mss-clamping-ip6-out",
403 };
404 
405 /*
406  * fd.io coding-style-patch-verification: ON
407  *
408  * Local Variables:
409  * eval: (c-set-style "gnu")
410  * End:
411  */
mssc_trace_t
struct mssc_trace_t_ mssc_trace_t
vlib.h
tcp_doff
#define tcp_doff(_th)
Definition: tcp_packet.h:78
TCP_OPTION_EOL
@ TCP_OPTION_EOL
End of options.
Definition: tcp_packet.h:104
bufs
vlib_buffer_t * bufs[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:717
vlib_prefetch_buffer_header
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:231
frame
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: nat44_ei.c:3048
pg.h
ip6_header_t::protocol
u8 protocol
Definition: ip6_packet.h:304
tcp_header_t
struct _tcp_header tcp_header_t
vlib_get_buffers
vlib_get_buffers(vm, from, b, n_left_from)
next
u16 * next
Definition: nat44_ei_out2in.c:718
VLIB_NODE_TYPE_INTERNAL
@ VLIB_NODE_TYPE_INTERNAL
Definition: node.h:72
mss_clamp.h
VLIB_FRAME_SIZE
#define VLIB_FRAME_SIZE
Definition: node.h:368
node
vlib_main_t vlib_node_runtime_t * node
Definition: nat44_ei.c:3047
mssc_trace_t_
Definition: mss_clamp_node.c:31
u16
unsigned short u16
Definition: types.h:57
TCP_OPTION_NOOP
@ TCP_OPTION_NOOP
No operation.
Definition: tcp_packet.h:105
vm
vlib_main_t * vm
X-connect all packets from the HOST to the PHY.
Definition: nat44_ei.c:3047
VLIB_RX
@ VLIB_RX
Definition: defs.h:46
vlib_buffer_enqueue_to_next
vlib_buffer_enqueue_to_next(vm, node, from,(u16 *) nexts, frame->n_vectors)
ip6_next_header
static void * ip6_next_header(ip6_header_t *i)
Definition: ip6_packet.h:407
vlib_frame_t
Definition: node.h:372
format_mssc_trace
static u8 * format_mssc_trace(u8 *s, va_list *args)
Definition: mss_clamp_node.c:39
MSSC_N_NEXT
@ MSSC_N_NEXT
Definition: mss_clamp_node.c:52
ip4_header_t
Definition: ip4_packet.h:87
tcp_syn
#define tcp_syn(_th)
Definition: tcp_packet.h:80
MSSC_NEXT_DROP
@ MSSC_NEXT_DROP
Definition: mss_clamp_node.c:51
error.h
feature.h
CLIB_UNUSED
#define CLIB_UNUSED(x)
Definition: clib.h:90
vnet_buffer
#define vnet_buffer(b)
Definition: buffer.h:437
vnet_feature_next_u16
static_always_inline void vnet_feature_next_u16(u16 *next0, vlib_buffer_t *b0)
Definition: feature.h:328
VLIB_NODE_FLAG_TRACE
#define VLIB_NODE_FLAG_TRACE
Definition: node.h:291
PREDICT_FALSE
#define PREDICT_FALSE(x)
Definition: clib.h:124
fib_types.h
vlib_frame_vector_args
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:301
uword
u64 uword
Definition: types.h:112
vlib_rx_or_tx_t
vlib_rx_or_tx_t
Definition: defs.h:44
mssc_ip6_out
static uword mssc_ip6_out(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: mss_clamp_node.c:310
mssc_ip6_out_node
vlib_node_registration_t mssc_ip6_out_node
(constructor) VLIB_REGISTER_NODE (mssc_ip6_out_node)
Definition: mss_clamp_node.c:29
vlib_node_increment_counter
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1244
cm
vnet_feature_config_main_t * cm
Definition: nat44_ei_hairpinning.c:591
fib_protocol_t
enum fib_protocol_t_ fib_protocol_t
Protocol Type.
FIB_PROTOCOL_IP4
@ FIB_PROTOCOL_IP4
Definition: fib_types.h:36
VNET_FEATURE_INIT
VNET_FEATURE_INIT(mssc_ip4_in_feat, static)
vlib_node_registration_t
struct _vlib_node_registration vlib_node_registration_t
mssc_ip4_in_node
vlib_node_registration_t mssc_ip4_in_node
(constructor) VLIB_REGISTER_NODE (mssc_ip4_in_node)
Definition: mss_clamp_node.c:315
mssc_ip6_in_node
vlib_node_registration_t mssc_ip6_in_node
(constructor) VLIB_REGISTER_NODE (mssc_ip6_in_node)
Definition: mss_clamp_node.c:349
data
u8 data[128]
Definition: ipsec_types.api:92
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
mssc_ip4_in
static uword mssc_ip4_in(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: mss_clamp_node.c:292
format
description fragment has unexpected format
Definition: map.api:433
mssc_ip6_in
static uword mssc_ip6_in(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: mss_clamp_node.c:304
u32
unsigned int u32
Definition: types.h:88
mssc_trace_t_::clamped
u32 clamped
Definition: mss_clamp_node.c:34
FIB_PROTOCOL_IP6
@ FIB_PROTOCOL_IP6
Definition: fib_types.h:37
mssc_inline
static uword mssc_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, vlib_dir_t dir, fib_protocol_t fproto)
Definition: mss_clamp_node.c:118
TCP_OPTION_MSS
@ TCP_OPTION_MSS
Limit MSS.
Definition: tcp_packet.h:106
n_left
u32 n_left
Definition: interface_output.c:1078
mssc_ip4_out_node
vlib_node_registration_t mssc_ip4_out_node
(constructor) VLIB_REGISTER_NODE (mssc_ip4_out_node)
Definition: mss_clamp_node.c:28
ip6_header_t
Definition: ip6_packet.h:294
ip4.h
vlib_main_t
Definition: main.h:102
ip_csum_update
#define ip_csum_update(sum, old, new, type, field)
Definition: ip_packet.h:295
vlib_node_t
Definition: node.h:247
vlib_add_trace
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:628
b
vlib_buffer_t ** b
Definition: nat44_ei_out2in.c:717
VNET_FEATURES
#define VNET_FEATURES(...)
Definition: feature.h:470
mssc_main_t
Definition: mss_clamp.h:29
u8
unsigned char u8
Definition: types.h:56
vlib_buffer_get_current
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:257
ip_csum_t
uword ip_csum_t
Definition: ip_packet.h:245
mssc_ip4_out
static uword mssc_ip4_out(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: mss_clamp_node.c:298
mssc_mss_fixup
static u32 mssc_mss_fixup(vlib_buffer_t *b0, tcp_header_t *tcp0, u16 max_mss0)
Definition: mss_clamp_node.c:60
nexts
u16 nexts[VLIB_FRAME_SIZE]
Definition: nat44_ei_out2in.c:718
vnet.h
vlib_node_runtime_t
Definition: node.h:454
from
from
Definition: nat44_ei_hairpinning.c:415
mssc_trace_t_::max_mss
u32 max_mss
Definition: mss_clamp_node.c:33
ip6.h
ip_csum_fold
static u16 ip_csum_fold(ip_csum_t c)
Definition: ip_packet.h:301
VLIB_TX
@ VLIB_TX
Definition: defs.h:47
mssc_next_t
mssc_next_t
Definition: mss_clamp_node.c:49
type
vl_api_fib_path_type_t type
Definition: fib_types.api:123
ip4_header_t::protocol
u8 protocol
Definition: ip4_packet.h:115
vlib_prefetch_buffer_data
#define vlib_prefetch_buffer_data(b, type)
Definition: buffer.h:232
mssc_main
mssc_main_t mssc_main
Definition: mss_clamp.c:23
ip4_next_header
static void * ip4_next_header(ip4_header_t *i)
Definition: ip4_packet.h:196
vlib_buffer_t::flags
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index,...
Definition: buffer.h:133
vlib_buffer_t
VLIB buffer representation.
Definition: buffer.h:111
VLIB_REGISTER_NODE
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
flags
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:105