blob: 09bf468ac40b212690dd783e455c5de911df459b [file] [log] [blame]
Neale Ranns5e575b12016-10-03 09:40:25 +01001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <vnet/vnet.h>
17#include <vnet/adj/adj_l2.h>
18#include <vnet/ethernet/ethernet.h>
19#include <vnet/ip/ip.h>
20
21/**
22 * @brief Trace data for a L2 Midchain
23 */
24typedef struct adj_l2_trace_t_ {
25 /** Adjacency index taken. */
26 u32 adj_index;
27} adj_l2_trace_t;
28
29static u8 *
30format_adj_l2_trace (u8 * s, va_list * args)
31{
32 CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
33 CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
34 adj_l2_trace_t * t = va_arg (*args, adj_l2_trace_t *);
Neale Ranns5e575b12016-10-03 09:40:25 +010035
36 s = format (s, "adj-idx %d : %U",
37 t->adj_index,
Neale Rannsb80c5362016-10-08 13:03:40 +010038 format_ip_adjacency, t->adj_index, FORMAT_IP_ADJACENCY_NONE);
Neale Ranns5e575b12016-10-03 09:40:25 +010039 return s;
40}
41
42typedef enum adj_l2_rewrite_next_t_
43{
44 ADJ_L2_REWRITE_NEXT_DROP,
45} adj_l2_rewrite_next_t;
46
47always_inline uword
48adj_l2_rewrite_inline (vlib_main_t * vm,
49 vlib_node_runtime_t * node,
50 vlib_frame_t * frame,
51 int is_midchain)
52{
53 u32 * from = vlib_frame_vector_args (frame);
54 u32 n_left_from, n_left_to_next, * to_next, next_index;
Damjan Marion586afd72017-04-05 19:18:20 +020055 u32 thread_index = vlib_get_thread_index();
Damjan Marion8b3191e2016-11-09 19:54:20 +010056 ethernet_main_t * em = &ethernet_main;
Neale Ranns5e575b12016-10-03 09:40:25 +010057
58 n_left_from = frame->n_vectors;
59 next_index = node->cached_next_index;
60
61 while (n_left_from > 0)
62 {
63 vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
64
65 while (n_left_from > 0 && n_left_to_next > 0)
66 {
67 ip_adjacency_t * adj0;
68 vlib_buffer_t * p0;
69 char *h0;
Damjan Marion8b3191e2016-11-09 19:54:20 +010070 u32 pi0, rw_len0, adj_index0, next0 = 0;
Neale Ranns5e575b12016-10-03 09:40:25 +010071 u32 tx_sw_if_index0;
72
73 pi0 = to_next[0] = from[0];
74 from += 1;
75 n_left_from -= 1;
76 to_next += 1;
77 n_left_to_next -= 1;
78
79 p0 = vlib_get_buffer (vm, pi0);
80 h0 = vlib_buffer_get_current (p0);
81
82 adj_index0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
83
Neale Ranns5e575b12016-10-03 09:40:25 +010084 adj0 = adj_get (adj_index0);
85
86 /* Guess we are only writing on simple Ethernet header. */
87 vnet_rewrite_one_header (adj0[0], h0,
88 sizeof (ethernet_header_t));
89
90 /* Update packet buffer attributes/set output interface. */
91 rw_len0 = adj0[0].rewrite_header.data_bytes;
92 vnet_buffer(p0)->ip.save_rewrite_length = rw_len0;
Filip Tehlar4868ff62017-03-09 16:48:39 +010093 vnet_buffer(p0)->sw_if_index[VLIB_TX] = adj0->rewrite_header.sw_if_index;
Neale Ranns31ed7442018-02-23 05:29:09 -080094 /* since we are coming out of the L2 world, where the vlib_buffer
95 * union is used for other things, make sure it is clean for
96 * MPLS from now on.
97 */
98 vnet_buffer(p0)->mpls.first = 0;
Neale Ranns5e575b12016-10-03 09:40:25 +010099
Neale Ranns044183f2017-01-24 01:34:25 -0800100 vlib_increment_combined_counter(&adjacency_counters,
Damjan Marion586afd72017-04-05 19:18:20 +0200101 thread_index,
Neale Ranns044183f2017-01-24 01:34:25 -0800102 adj_index0,
103 /* packet increment */ 0,
104 /* byte increment */ rw_len0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100105
106 /* Check MTU of outgoing interface. */
107 if (PREDICT_TRUE((vlib_buffer_length_in_chain (vm, p0) <=
108 adj0[0].rewrite_header.max_l3_packet_bytes)))
109 {
110 /* Don't adjust the buffer for ttl issue; icmp-error node wants
111 * to see the IP headerr */
112 p0->current_data -= rw_len0;
113 p0->current_length += rw_len0;
114 tx_sw_if_index0 = adj0[0].rewrite_header.sw_if_index;
115
116 if (is_midchain)
117 {
Neale Rannsdb14f5a2018-01-29 10:43:33 -0800118 adj0->sub_type.midchain.fixup_func(
119 vm, adj0, p0,
120 adj0->sub_type.midchain.fixup_data);
Neale Ranns5e575b12016-10-03 09:40:25 +0100121 }
122
123 vnet_buffer (p0)->sw_if_index[VLIB_TX] = tx_sw_if_index0;
124
125 /*
126 * Follow the feature ARC. this will result eventually in
127 * the midchain-tx node
128 */
Damjan Marion8b3191e2016-11-09 19:54:20 +0100129 vnet_feature_arc_start(em->output_feature_arc_index, tx_sw_if_index0, &next0, p0);
Neale Ranns5e575b12016-10-03 09:40:25 +0100130 }
131 else
132 {
133 /* can't fragment L2 */
134 next0 = ADJ_L2_REWRITE_NEXT_DROP;
135 }
136
137 if (PREDICT_FALSE(p0->flags & VLIB_BUFFER_IS_TRACED))
138 {
139 adj_l2_trace_t *tr = vlib_add_trace (vm, node,
140 p0, sizeof (*tr));
141 tr->adj_index = vnet_buffer(p0)->ip.adj_index[VLIB_TX];
142 }
143
144 vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
145 to_next, n_left_to_next,
146 pi0, next0);
147 }
148
149 vlib_put_next_frame (vm, node, next_index, n_left_to_next);
150 }
151
152 return frame->n_vectors;
153}
154
155static uword
156adj_l2_rewrite (vlib_main_t * vm,
157 vlib_node_runtime_t * node,
158 vlib_frame_t * frame)
159{
160 return adj_l2_rewrite_inline (vm, node, frame, 0);
161}
162
163static uword
164adj_l2_midchain (vlib_main_t * vm,
165 vlib_node_runtime_t * node,
166 vlib_frame_t * frame)
167{
168 return adj_l2_rewrite_inline (vm, node, frame, 1);
169}
170
171VLIB_REGISTER_NODE (adj_l2_rewrite_node) = {
172 .function = adj_l2_rewrite,
173 .name = "adj-l2-rewrite",
174 .vector_size = sizeof (u32),
175
176 .format_trace = format_adj_l2_trace,
177
178 .n_next_nodes = 1,
179 .next_nodes = {
180 [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop",
181 },
182};
183
184VLIB_NODE_FUNCTION_MULTIARCH (adj_l2_rewrite_node, adj_l2_rewrite)
185
186VLIB_REGISTER_NODE (adj_l2_midchain_node) = {
187 .function = adj_l2_midchain,
188 .name = "adj-l2-midchain",
189 .vector_size = sizeof (u32),
190
191 .format_trace = format_adj_l2_trace,
192
193 .n_next_nodes = 1,
194 .next_nodes = {
195 [ADJ_L2_REWRITE_NEXT_DROP] = "error-drop",
196 },
197};
198
199VLIB_NODE_FUNCTION_MULTIARCH (adj_l2_midchain_node, adj_l2_midchain)