blob: 561c86cdfa46f037734189bd1fba472e75e3521d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * replication.c : packet replication
3 *
4 * Copyright (c) 2013 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
18#include <vlib/vlib.h>
19#include <vnet/vnet.h>
20#include <vppinfra/error.h>
21#include <vnet/ip/ip4_packet.h>
22#include <vnet/replication.h>
23
24
25replication_main_t replication_main;
26
27
28replication_context_t *
29replication_prep (vlib_main_t * vm,
Dave Barachba868bb2016-08-08 09:51:21 -040030 vlib_buffer_t * b0, u32 recycle_node_index, u32 l2_packet)
Ed Warnickecb9cada2015-12-08 15:45:58 -070031{
Dave Barachba868bb2016-08-08 09:51:21 -040032 replication_main_t *rm = &replication_main;
33 replication_context_t *ctx;
Ed Warnickecb9cada2015-12-08 15:45:58 -070034 uword cpu_number = vm->cpu_index;
Dave Barachba868bb2016-08-08 09:51:21 -040035 ip4_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -070036 u32 ctx_id;
37
Dave Barachba868bb2016-08-08 09:51:21 -040038 /* Allocate a context, reserve context 0 */
39 if (PREDICT_FALSE (rm->contexts[cpu_number] == 0))
Ed Warnickecb9cada2015-12-08 15:45:58 -070040 pool_get_aligned (rm->contexts[cpu_number], ctx, CLIB_CACHE_LINE_BYTES);
Dave Barachba868bb2016-08-08 09:51:21 -040041
Ed Warnickecb9cada2015-12-08 15:45:58 -070042 pool_get_aligned (rm->contexts[cpu_number], ctx, CLIB_CACHE_LINE_BYTES);
43 ctx_id = ctx - rm->contexts[cpu_number];
44
Dave Barachba868bb2016-08-08 09:51:21 -040045 /* Save state from vlib buffer */
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 ctx->saved_free_list_index = b0->free_list_index;
47 ctx->current_data = b0->current_data;
48
Dave Barachba868bb2016-08-08 09:51:21 -040049 /* Set up vlib buffer hooks */
Dave Barachb5adaea2016-06-17 14:09:56 -040050 b0->recycle_count = ctx_id;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 b0->free_list_index = rm->recycle_list_index;
Dave Barachb5adaea2016-06-17 14:09:56 -040052 b0->flags |= VLIB_BUFFER_RECYCLE;
Ed Warnickecb9cada2015-12-08 15:45:58 -070053
Dave Barachba868bb2016-08-08 09:51:21 -040054 /* Save feature state */
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 ctx->recycle_node_index = recycle_node_index;
56
Dave Barachba868bb2016-08-08 09:51:21 -040057 /* Save vnet state */
58 clib_memcpy (ctx->vnet_buffer, vnet_buffer (b0),
59 sizeof (vnet_buffer_opaque_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Dave Barachba868bb2016-08-08 09:51:21 -040061 /* Save packet contents */
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 ctx->l2_packet = l2_packet;
Dave Barachba868bb2016-08-08 09:51:21 -040063 ip = (ip4_header_t *) vlib_buffer_get_current (b0);
64 if (l2_packet)
65 {
66 /* Save ethernet header */
67 ctx->l2_header[0] = ((u64 *) ip)[0];
68 ctx->l2_header[1] = ((u64 *) ip)[1];
69 ctx->l2_header[2] = ((u64 *) ip)[2];
70 /* set ip to the true ip header */
71 ip = (ip4_header_t *) (((u8 *) ip) + vnet_buffer (b0)->l2.l2_len);
72 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070073
Dave Barachba868bb2016-08-08 09:51:21 -040074 /*
75 * Copy L3 fields.
76 * We need to save TOS for ip4 and ip6 packets.
77 * Fortunately the TOS field is
78 * in the first two bytes of both the ip4 and ip6 headers.
79 */
80 ctx->ip_tos = *((u16 *) (ip));
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Dave Barachba868bb2016-08-08 09:51:21 -040082 /*
83 * Save the ip4 checksum as well. We just blindly save the corresponding two
84 * bytes even for ip6 packets.
85 */
Ed Warnickecb9cada2015-12-08 15:45:58 -070086 ctx->ip4_checksum = ip->checksum;
87
88 return ctx;
89}
90
91
92replication_context_t *
Dave Barachba868bb2016-08-08 09:51:21 -040093replication_recycle (vlib_main_t * vm, vlib_buffer_t * b0, u32 is_last)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
Dave Barachba868bb2016-08-08 09:51:21 -040095 replication_main_t *rm = &replication_main;
96 replication_context_t *ctx;
Ed Warnickecb9cada2015-12-08 15:45:58 -070097 uword cpu_number = vm->cpu_index;
Dave Barachba868bb2016-08-08 09:51:21 -040098 ip4_header_t *ip;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Dave Barachba868bb2016-08-08 09:51:21 -0400100 /* Get access to the replication context */
Dave Barachb5adaea2016-06-17 14:09:56 -0400101 ctx = pool_elt_at_index (rm->contexts[cpu_number], b0->recycle_count);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102
Dave Barachba868bb2016-08-08 09:51:21 -0400103 /* Restore vnet buffer state */
104 clib_memcpy (vnet_buffer (b0), ctx->vnet_buffer,
105 sizeof (vnet_buffer_opaque_t));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106
Dave Barachba868bb2016-08-08 09:51:21 -0400107 /* Restore the packet start (current_data) and length */
108 vlib_buffer_advance (b0, ctx->current_data - b0->current_data);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109
Dave Barachba868bb2016-08-08 09:51:21 -0400110 /* Restore packet contents */
111 ip = (ip4_header_t *) vlib_buffer_get_current (b0);
112 if (ctx->l2_packet)
113 {
114 /* Restore ethernet header */
115 ((u64 *) ip)[0] = ctx->l2_header[0];
116 ((u64 *) ip)[1] = ctx->l2_header[1];
117 ((u64 *) ip)[2] = ctx->l2_header[2];
118 /* set ip to the true ip header */
119 ip = (ip4_header_t *) (((u8 *) ip) + vnet_buffer (b0)->l2.l2_len);
120 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 // Restore L3 fields
Dave Barachba868bb2016-08-08 09:51:21 -0400123 *((u16 *) (ip)) = ctx->ip_tos;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 ip->checksum = ctx->ip4_checksum;
125
Dave Barachba868bb2016-08-08 09:51:21 -0400126 if (is_last)
127 {
128 /*
129 * This is the last replication in the list.
130 * Restore original buffer free functionality.
131 */
132 b0->free_list_index = ctx->saved_free_list_index;
133 b0->flags &= ~VLIB_BUFFER_RECYCLE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barachba868bb2016-08-08 09:51:21 -0400135 /* Free context back to its pool */
136 pool_put (rm->contexts[cpu_number], ctx);
137 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138
139 return ctx;
140}
141
142
143
144/*
145 * fish pkts back from the recycle queue/freelist
146 * un-flatten the context chains
147 */
Dave Barachba868bb2016-08-08 09:51:21 -0400148static void
149replication_recycle_callback (vlib_main_t * vm, vlib_buffer_free_list_t * fl)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150{
Dave Barachba868bb2016-08-08 09:51:21 -0400151 vlib_frame_t *f = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 u32 n_left_from;
153 u32 n_left_to_next = 0;
154 u32 n_this_frame = 0;
Dave Barachba868bb2016-08-08 09:51:21 -0400155 u32 *from;
156 u32 *to_next = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 u32 bi0, pi0;
158 vlib_buffer_t *b0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159 int i;
Dave Barachba868bb2016-08-08 09:51:21 -0400160 replication_main_t *rm = &replication_main;
161 replication_context_t *ctx;
162 u32 feature_node_index = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700163 uword cpu_number = vm->cpu_index;
164
Dave Barachba868bb2016-08-08 09:51:21 -0400165 /*
166 * All buffers in the list are destined to the same recycle node.
167 * Pull the recycle node index from the first buffer.
168 * Note: this could be sped up if the node index were stuffed into
169 * the freelist itself.
170 */
171 if (vec_len (fl->aligned_buffers) > 0)
172 {
173 bi0 = fl->aligned_buffers[0];
174 b0 = vlib_get_buffer (vm, bi0);
175 ctx = pool_elt_at_index (rm->contexts[cpu_number], b0->recycle_count);
176 feature_node_index = ctx->recycle_node_index;
177 }
178 else if (vec_len (fl->unaligned_buffers) > 0)
179 {
180 bi0 = fl->unaligned_buffers[0];
181 b0 = vlib_get_buffer (vm, bi0);
182 ctx = pool_elt_at_index (rm->contexts[cpu_number], b0->recycle_count);
183 feature_node_index = ctx->recycle_node_index;
184 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
186 /* aligned, unaligned buffers */
Dave Barachba868bb2016-08-08 09:51:21 -0400187 for (i = 0; i < 2; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188 {
189 if (i == 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400190 {
191 from = fl->aligned_buffers;
192 n_left_from = vec_len (from);
193 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194 else
Dave Barachba868bb2016-08-08 09:51:21 -0400195 {
196 from = fl->unaligned_buffers;
197 n_left_from = vec_len (from);
198 }
199
Ed Warnickecb9cada2015-12-08 15:45:58 -0700200 while (n_left_from > 0)
Dave Barachba868bb2016-08-08 09:51:21 -0400201 {
202 if (PREDICT_FALSE (n_left_to_next == 0))
203 {
204 if (f)
205 {
206 f->n_vectors = n_this_frame;
207 vlib_put_frame_to_node (vm, feature_node_index, f);
208 }
209
210 f = vlib_get_frame_to_node (vm, feature_node_index);
211 to_next = vlib_frame_vector_args (f);
212 n_left_to_next = VLIB_FRAME_SIZE;
213 n_this_frame = 0;
214 }
215
216 bi0 = from[0];
217 if (PREDICT_TRUE (n_left_from > 1))
218 {
219 pi0 = from[1];
220 vlib_prefetch_buffer_with_index (vm, pi0, LOAD);
221 }
John Lo17f17cb2016-05-31 16:25:13 -0400222
223 b0 = vlib_get_buffer (vm, bi0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
Dave Barachba868bb2016-08-08 09:51:21 -0400225 /* Mark that this buffer was just recycled */
226 b0->flags |= VLIB_BUFFER_IS_RECYCLED;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
Neale Rannsb80c5362016-10-08 13:03:40 +0100228#if (CLIB_DEBUG > 0)
229#if DPDK == 0
230 vlib_buffer_set_known_state (vm, bi0, VLIB_BUFFER_KNOWN_ALLOCATED);
231#endif
232#endif
233
Dave Barachba868bb2016-08-08 09:51:21 -0400234 /* If buffer is traced, mark frame as traced */
235 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
236 f->flags |= VLIB_FRAME_TRACE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
Dave Barachba868bb2016-08-08 09:51:21 -0400238 to_next[0] = bi0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239
Dave Barachba868bb2016-08-08 09:51:21 -0400240 from++;
241 to_next++;
242 n_this_frame++;
243 n_left_to_next--;
244 n_left_from--;
245 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 }
Dave Barachba868bb2016-08-08 09:51:21 -0400247
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 vec_reset_length (fl->aligned_buffers);
249 vec_reset_length (fl->unaligned_buffers);
250
251 if (f)
252 {
Dave Barachba868bb2016-08-08 09:51:21 -0400253 ASSERT (n_this_frame);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254 f->n_vectors = n_this_frame;
255 vlib_put_frame_to_node (vm, feature_node_index, f);
256 }
257}
258
Dave Barachba868bb2016-08-08 09:51:21 -0400259clib_error_t *
260replication_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261{
Dave Barachba868bb2016-08-08 09:51:21 -0400262 replication_main_t *rm = &replication_main;
263 vlib_buffer_main_t *bm = vm->buffer_main;
264 vlib_buffer_free_list_t *fl;
265 __attribute__ ((unused)) replication_context_t *ctx;
266 vlib_thread_main_t *tm = vlib_get_thread_main ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Dave Barachba868bb2016-08-08 09:51:21 -0400268 rm->vlib_main = vm;
269 rm->vnet_main = vnet_get_main ();
270 rm->recycle_list_index =
271 vlib_buffer_create_free_list (vm, 1024 /* fictional */ ,
272 "replication-recycle");
273
274 fl = pool_elt_at_index (bm->buffer_free_list_pool, rm->recycle_list_index);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275
276 fl->buffers_added_to_freelist_function = replication_recycle_callback;
277
Dave Barachba868bb2016-08-08 09:51:21 -0400278 /* Verify the replication context is the expected size */
279 ASSERT (sizeof (replication_context_t) == 128); /* 2 cache lines */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281 vec_validate (rm->contexts, tm->n_vlib_mains - 1);
282 return 0;
283}
284
285VLIB_INIT_FUNCTION (replication_init);
Dave Barachba868bb2016-08-08 09:51:21 -0400286
287/*
288 * fd.io coding-style-patch-verification: ON
289 *
290 * Local Variables:
291 * eval: (c-set-style "gnu")
292 * End:
293 */