2 * mpls_output.c: MPLS Adj rewrite
4 * Copyright (c) 2012-2014 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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/mpls/mpls.h>
23 /* Adjacency taken. */
27 /* Packet data, possibly *after* rewrite. */
28 u8 packet_data[64 - 1*sizeof(u32)];
29 } mpls_output_trace_t;
32 format_mpls_output_trace (u8 * s, va_list * args)
34 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
35 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
36 mpls_output_trace_t * t = va_arg (*args, mpls_output_trace_t *);
37 vnet_main_t * vnm = vnet_get_main();
38 uword indent = format_get_indent (s);
40 s = format (s, "adj-idx %d : %U flow hash: 0x%08x",
42 format_ip_adjacency, vnm, t->adj_index, FORMAT_IP_ADJACENCY_NONE,
44 s = format (s, "\n%U%U",
45 format_white_space, indent,
46 format_ip_adjacency_packet_data,
48 t->packet_data, sizeof (t->packet_data));
53 mpls_output_inline (vlib_main_t * vm,
54 vlib_node_runtime_t * node,
55 vlib_frame_t * from_frame)
57 u32 n_left_from, next_index, * from, * to_next, cpu_index;
58 vlib_node_runtime_t * error_node;
60 cpu_index = os_get_cpu_number();
61 error_node = vlib_node_get_runtime (vm, mpls_output_node.index);
62 from = vlib_frame_vector_args (from_frame);
63 n_left_from = from_frame->n_vectors;
64 next_index = node->cached_next_index;
66 while (n_left_from > 0)
70 vlib_get_next_frame (vm, node, next_index,
71 to_next, n_left_to_next);
73 while (n_left_from > 0 && n_left_to_next > 0)
75 ip_adjacency_t * adj0;
76 mpls_unicast_header_t *hdr0;
78 u32 pi0, rw_len0, adj_index0, next0, error0;
80 pi0 = to_next[0] = from[0];
82 p0 = vlib_get_buffer (vm, pi0);
84 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
86 /* We should never rewrite a pkt using the MISS adjacency */
89 adj0 = adj_get(adj_index0);
90 hdr0 = vlib_buffer_get_current (p0);
92 /* Guess we are only writing on simple Ethernet header. */
93 vnet_rewrite_one_header (adj0[0], hdr0,
94 sizeof (ethernet_header_t));
96 /* Update packet buffer attributes/set output interface. */
97 rw_len0 = adj0[0].rewrite_header.data_bytes;
99 if (PREDICT_FALSE (rw_len0 > sizeof(ethernet_header_t)))
100 vlib_increment_combined_counter
101 (&adjacency_counters,
102 cpu_index, adj_index0,
103 /* packet increment */ 0,
104 /* byte increment */ rw_len0-sizeof(ethernet_header_t));
106 /* Check MTU of outgoing interface. */
107 error0 = (vlib_buffer_length_in_chain (vm, p0)
108 > adj0[0].rewrite_header.max_l3_packet_bytes
109 ? IP4_ERROR_MTU_EXCEEDED
112 p0->error = error_node->errors[error0];
114 /* Don't adjust the buffer for ttl issue; icmp-error node wants
115 * to see the IP headerr */
116 if (PREDICT_TRUE(error0 == IP4_ERROR_NONE))
118 p0->current_data -= rw_len0;
119 p0->current_length += rw_len0;
121 vnet_buffer (p0)->sw_if_index[VLIB_TX] =
122 adj0[0].rewrite_header.sw_if_index;
123 next0 = adj0[0].rewrite_header.next_index;
127 next0 = MPLS_OUTPUT_NEXT_DROP;
135 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
137 mpls_output_trace_t *tr = vlib_add_trace (vm, node,
139 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
140 tr->flow_hash = vnet_buffer(p0)->ip.flow_hash;
143 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
144 to_next, n_left_to_next,
148 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
150 vlib_node_increment_counter (vm, mpls_output_node.index,
151 MPLS_ERROR_PKTS_ENCAP,
152 from_frame->n_vectors);
154 return from_frame->n_vectors;
157 static char * mpls_error_strings[] = {
158 #define mpls_error(n,s) s,
164 mpls_output (vlib_main_t * vm,
165 vlib_node_runtime_t * node,
166 vlib_frame_t * from_frame)
168 return (mpls_output_inline(vm, node, from_frame));
171 VLIB_REGISTER_NODE (mpls_output_node) = {
172 .function = mpls_output,
173 .name = "mpls-output",
174 /* Takes a vector of packets. */
175 .vector_size = sizeof (u32),
176 .n_errors = MPLS_N_ERROR,
177 .error_strings = mpls_error_strings,
179 .n_next_nodes = MPLS_OUTPUT_N_NEXT,
181 #define _(s,n) [MPLS_OUTPUT_NEXT_##s] = n,
182 foreach_mpls_output_next
186 .format_trace = format_mpls_output_trace,
189 VLIB_NODE_FUNCTION_MULTIARCH (mpls_output_node, mpls_output)
192 mpls_midchain (vlib_main_t * vm,
193 vlib_node_runtime_t * node,
194 vlib_frame_t * from_frame)
196 return (mpls_output_inline(vm, node, from_frame));
199 VLIB_REGISTER_NODE (mpls_midchain_node) = {
200 .function = mpls_output,
201 .name = "mpls-midchain",
202 .vector_size = sizeof (u32),
204 .format_trace = format_mpls_output_trace,
206 .sibling_of = "mpls-output",
209 VLIB_NODE_FUNCTION_MULTIARCH (mpls_midchain_node, mpls_midchain)
212 * @brief Next index values from the MPLS incomplete adj node
214 #define foreach_mpls_adj_incomplete_next \
215 _(DROP, "error-drop") \
217 _(IP6, "ip6-discover-neighbor")
220 #define _(s,n) MPLS_ADJ_INCOMPLETE_NEXT_##s,
221 foreach_mpls_adj_incomplete_next
223 MPLS_ADJ_INCOMPLETE_N_NEXT,
224 } mpls_adj_incomplete_next_t;
227 * @brief A struct to hold tracing information for the MPLS label imposition
230 typedef struct mpls_adj_incomplete_trace_t_
233 } mpls_adj_incomplete_trace_t;
237 * @brief Graph node for incomplete MPLS adjacency.
238 * This node will push traffic to either the v4-arp or v6-nd node
239 * based on the next-hop proto of the adj.
240 * We pay a cost for this 'routing' node, but an incomplete adj is the
244 mpls_adj_incomplete (vlib_main_t * vm,
245 vlib_node_runtime_t * node,
246 vlib_frame_t * from_frame)
248 u32 n_left_from, next_index, * from, * to_next;
250 from = vlib_frame_vector_args (from_frame);
251 n_left_from = from_frame->n_vectors;
252 next_index = node->cached_next_index;
254 while (n_left_from > 0)
258 vlib_get_next_frame (vm, node, next_index,
259 to_next, n_left_to_next);
261 while (n_left_from > 0 && n_left_to_next > 0)
263 u32 pi0, next0, adj_index0;
264 ip_adjacency_t * adj0;
267 pi0 = to_next[0] = from[0];
268 p0 = vlib_get_buffer (vm, pi0);
274 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
277 adj0 = adj_get(adj_index0);
279 if (PREDICT_TRUE(FIB_PROTOCOL_IP4 == adj0->ia_nh_proto))
281 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP4;
285 next0 = MPLS_ADJ_INCOMPLETE_NEXT_IP6;
288 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
290 mpls_adj_incomplete_trace_t *tr =
291 vlib_add_trace (vm, node, p0, sizeof (*tr));
295 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
296 to_next, n_left_to_next,
300 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
303 return from_frame->n_vectors;
307 format_mpls_adj_incomplete_trace (u8 * s, va_list * args)
309 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
310 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
311 mpls_adj_incomplete_trace_t * t;
314 t = va_arg (*args, mpls_adj_incomplete_trace_t *);
315 indent = format_get_indent (s);
317 s = format (s, "%Unext:%d",
318 format_white_space, indent,
323 VLIB_REGISTER_NODE (mpls_adj_incomplete_node) = {
324 .function = mpls_adj_incomplete,
325 .name = "mpls-adj-incomplete",
326 .format_trace = format_mpls_adj_incomplete_trace,
327 /* Takes a vector of packets. */
328 .vector_size = sizeof (u32),
329 .n_errors = MPLS_N_ERROR,
330 .error_strings = mpls_error_strings,
332 .n_next_nodes = MPLS_ADJ_INCOMPLETE_N_NEXT,
334 #define _(s,n) [MPLS_ADJ_INCOMPLETE_NEXT_##s] = n,
335 foreach_mpls_adj_incomplete_next
339 .format_trace = format_mpls_output_trace,
342 VLIB_NODE_FUNCTION_MULTIARCH (mpls_adj_incomplete_node,