blob: 33be4bbcae9ba78a40086809fbe9b33fb64ed1df [file] [log] [blame]
Neale Ranns8f5fef22020-12-21 08:29:34 +00001/*
2 *------------------------------------------------------------------
3 * ip_path_mtu.c
4 *
5 * Copyright (c) 2020 Graphiant.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at:
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *------------------------------------------------------------------
18 */
19
20#include <vnet/ip/ip_path_mtu.h>
21#include <vnet/ip/ip_frag.h>
22
23typedef enum
24{
25 IP_PMTU_DROP,
26 IP_PMTU_N_NEXT,
27} ip_pmtu_next_t;
28
29typedef struct ip_pmtu_trace_t_
30{
31 u16 pmtu;
32 u16 packet_size;
33} ip_pmtu_trace_t;
34
35static u8 *
36format_ip_pmtu_trace (u8 *s, va_list *args)
37{
38 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40 ip_pmtu_trace_t *t = va_arg (*args, ip_pmtu_trace_t *);
41
42 s = format (s, "path mtu:%d packet size:%d", t->pmtu, t->packet_size);
43
44 return s;
45}
46
47static inline uword
48ip_pmtu_dpo_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
49 vlib_frame_t *frame, ip_address_family_t af)
50{
51 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
52 u32 frag_sent = 0, small_packets = 0;
53
54 from = vlib_frame_vector_args (frame);
55 n_left_from = frame->n_vectors;
56 next_index = node->cached_next_index;
57
58 u32 *buffer = 0;
59
60 while (n_left_from > 0)
61 {
62 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
63
64 while (n_left_from > 0 && n_left_to_next > 0)
65 {
66 const ip_pmtu_dpo_t *ipm0;
67 u32 pi0, *frag_from, frag_left;
68 vlib_buffer_t *p0;
69 ip_frag_error_t error0;
70 u16 next0;
71
72 /*
73 * Note: The packet is not enqueued now. It is instead put
74 * in a vector where other fragments will be put as well.
75 */
76 pi0 = from[0];
77 from += 1;
78 n_left_from -= 1;
79
80 p0 = vlib_get_buffer (vm, pi0);
81 ipm0 = ip_pmtu_dpo_get (vnet_buffer (p0)->ip.adj_index[VLIB_TX]);
82 vnet_buffer (p0)->ip.adj_index[VLIB_TX] = ipm0->ipm_dpo.dpoi_index;
83 next0 = ipm0->ipm_dpo.dpoi_next_node;
84
85 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
86 {
87 ip_pmtu_trace_t *t;
88 t = vlib_add_trace (vm, node, p0, sizeof (*t));
89 t->pmtu = ipm0->ipm_pmtu;
90 t->packet_size = vlib_buffer_length_in_chain (vm, p0);
91 }
92
93 if (AF_IP6 == af)
94 error0 =
95 ip6_frag_do_fragment (vm, pi0, ipm0->ipm_pmtu, 0, &buffer);
96 else
97 error0 =
98 ip4_frag_do_fragment (vm, pi0, ipm0->ipm_pmtu, 0, &buffer);
99
100 if (AF_IP4 == af && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
101 {
102 icmp4_error_set_vnet_buffer (
103 p0, ICMP4_destination_unreachable,
104 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
105 ipm0->ipm_pmtu);
106 next0 = IP_FRAG_NEXT_ICMP_ERROR;
107 }
108 else
109 {
110 next0 =
111 (error0 == IP_FRAG_ERROR_NONE ? next0 : IP_FRAG_NEXT_DROP);
112 }
113
114 if (error0 == IP_FRAG_ERROR_NONE)
115 {
116 /* Free original buffer chain */
117 frag_sent += vec_len (buffer);
118 small_packets += (vec_len (buffer) == 1);
119 vlib_buffer_free_one (vm, pi0); /* Free original packet */
120 }
121 else
122 {
123 vlib_error_count (vm, node->node_index, error0, 1);
124 vec_add1 (buffer, pi0); /* Get rid of the original buffer */
125 }
126
127 /* Send fragments that were added in the frame */
128 frag_from = buffer;
129 frag_left = vec_len (buffer);
130
131 while (frag_left > 0)
132 {
133 while (frag_left > 0 && n_left_to_next > 0)
134 {
135 u32 i;
136 i = to_next[0] = frag_from[0];
137 frag_from += 1;
138 frag_left -= 1;
139 to_next += 1;
140 n_left_to_next -= 1;
141
142 vlib_get_buffer (vm, i)->error = node->errors[error0];
143 vlib_validate_buffer_enqueue_x1 (
144 vm, node, next_index, to_next, n_left_to_next, i, next0);
145 }
146 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
147 vlib_get_next_frame (vm, node, next_index, to_next,
148 n_left_to_next);
149 }
150 vec_reset_length (buffer);
151 }
152 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
153 }
154 vec_free (buffer);
155
156 return frame->n_vectors;
157}
158
159// clang-format off
160
161VLIB_NODE_FN (ip4_ip_pmtu_dpo_node) (vlib_main_t *vm,
162 vlib_node_runtime_t *node,
163 vlib_frame_t *from_frame)
164{
165 return (ip_pmtu_dpo_inline (vm, node, from_frame, 0));
166}
167
168VLIB_NODE_FN (ip6_ip_pmtu_dpo_node) (vlib_main_t *vm,
169 vlib_node_runtime_t *node,
170 vlib_frame_t *from_frame)
171{
172 return (ip_pmtu_dpo_inline (vm, node, from_frame, 1));
173}
174
175VLIB_REGISTER_NODE (ip4_ip_pmtu_dpo_node) = {
176 .name = "ip4-pmtu-dpo",
177 .vector_size = sizeof (u32),
178 .format_trace = format_ip_pmtu_trace,
Benoît Ganne43289952021-09-09 11:58:28 +0200179 .n_errors = IP_FRAG_N_ERROR,
180 .error_strings = ip4_frag_error_strings,
Neale Ranns8f5fef22020-12-21 08:29:34 +0000181 .n_next_nodes = IP_PMTU_N_NEXT,
182 .next_nodes =
183 {
184 [IP_PMTU_DROP] = "ip4-drop",
185 }
186};
187VLIB_REGISTER_NODE (ip6_ip_pmtu_dpo_node) = {
188 .name = "ip6-pmtu-dpo",
189 .vector_size = sizeof (u32),
190 .format_trace = format_ip_pmtu_trace,
Benoît Ganne43289952021-09-09 11:58:28 +0200191 .n_errors = IP_FRAG_N_ERROR,
192 .error_strings = ip4_frag_error_strings,
Neale Ranns8f5fef22020-12-21 08:29:34 +0000193 .n_next_nodes = IP_PMTU_N_NEXT,
194 .next_nodes =
195 {
196 [IP_PMTU_DROP] = "ip6-drop",
197 }
198};
199
200// clang-format on
201
202/*
203 * fd.io coding-style-patch-verification: ON
204 *
205 * Local Variables:
206 * eval: (c-set-style "gnu")
207 * End:
208 */