2 *------------------------------------------------------------------
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:
10 * http://www.apache.org/licenses/LICENSE-2.0
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 *------------------------------------------------------------------
20 #include <vnet/ip/ip_path_mtu.h>
21 #include <vnet/ip/ip_frag.h>
29 typedef struct ip_pmtu_trace_t_
36 format_ip_pmtu_trace (u8 *s, va_list *args)
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 *);
42 s = format (s, "path mtu:%d packet size:%d", t->pmtu, t->packet_size);
48 ip_pmtu_dpo_inline (vlib_main_t *vm, vlib_node_runtime_t *node,
49 vlib_frame_t *frame, ip_address_family_t af)
51 u32 n_left_from, *from, next_index, *to_next, n_left_to_next;
52 u32 frag_sent = 0, small_packets = 0;
54 from = vlib_frame_vector_args (frame);
55 n_left_from = frame->n_vectors;
56 next_index = node->cached_next_index;
60 while (n_left_from > 0)
62 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
64 while (n_left_from > 0 && n_left_to_next > 0)
66 const ip_pmtu_dpo_t *ipm0;
67 u32 pi0, *frag_from, frag_left;
69 ip_frag_error_t error0;
73 * Note: The packet is not enqueued now. It is instead put
74 * in a vector where other fragments will be put as well.
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;
85 if (PREDICT_FALSE (p0->flags & VLIB_BUFFER_IS_TRACED))
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);
95 ip6_frag_do_fragment (vm, pi0, ipm0->ipm_pmtu, 0, &buffer);
98 ip4_frag_do_fragment (vm, pi0, ipm0->ipm_pmtu, 0, &buffer);
100 if (AF_IP4 == af && error0 == IP_FRAG_ERROR_DONT_FRAGMENT_SET)
102 icmp4_error_set_vnet_buffer (
103 p0, ICMP4_destination_unreachable,
104 ICMP4_destination_unreachable_fragmentation_needed_and_dont_fragment_set,
106 next0 = IP_FRAG_NEXT_ICMP_ERROR;
111 (error0 == IP_FRAG_ERROR_NONE ? next0 : IP_FRAG_NEXT_DROP);
114 if (error0 == IP_FRAG_ERROR_NONE)
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 */
123 vlib_error_count (vm, node->node_index, error0, 1);
124 vec_add1 (buffer, pi0); /* Get rid of the original buffer */
127 /* Send fragments that were added in the frame */
129 frag_left = vec_len (buffer);
131 while (frag_left > 0)
133 while (frag_left > 0 && n_left_to_next > 0)
136 i = to_next[0] = frag_from[0];
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);
146 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
147 vlib_get_next_frame (vm, node, next_index, to_next,
150 vec_reset_length (buffer);
152 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
156 return frame->n_vectors;
161 VLIB_NODE_FN (ip4_ip_pmtu_dpo_node) (vlib_main_t *vm,
162 vlib_node_runtime_t *node,
163 vlib_frame_t *from_frame)
165 return (ip_pmtu_dpo_inline (vm, node, from_frame, 0));
168 VLIB_NODE_FN (ip6_ip_pmtu_dpo_node) (vlib_main_t *vm,
169 vlib_node_runtime_t *node,
170 vlib_frame_t *from_frame)
172 return (ip_pmtu_dpo_inline (vm, node, from_frame, 1));
175 VLIB_REGISTER_NODE (ip4_ip_pmtu_dpo_node) = {
176 .name = "ip4-pmtu-dpo",
177 .vector_size = sizeof (u32),
178 .format_trace = format_ip_pmtu_trace,
180 .n_next_nodes = IP_PMTU_N_NEXT,
183 [IP_PMTU_DROP] = "ip4-drop",
186 VLIB_REGISTER_NODE (ip6_ip_pmtu_dpo_node) = {
187 .name = "ip6-pmtu-dpo",
188 .vector_size = sizeof (u32),
189 .format_trace = format_ip_pmtu_trace,
191 .n_next_nodes = IP_PMTU_N_NEXT,
194 [IP_PMTU_DROP] = "ip6-drop",
201 * fd.io coding-style-patch-verification: ON
204 * eval: (c-set-style "gnu")