2 * replication.c : packet replication
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:
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/vnet.h>
20 #include <vppinfra/error.h>
21 #include <vnet/ip/ip4_packet.h>
22 #include <vnet/replication.h>
25 replication_main_t replication_main;
28 replication_context_t *
29 replication_prep (vlib_main_t * vm,
30 vlib_buffer_t * b0, u32 recycle_node_index, u32 l2_packet)
32 replication_main_t *rm = &replication_main;
33 replication_context_t *ctx;
34 uword thread_index = vm->thread_index;
38 /* Allocate a context, reserve context 0 */
39 if (PREDICT_FALSE (rm->contexts[thread_index] == 0))
40 pool_get_aligned (rm->contexts[thread_index], ctx, CLIB_CACHE_LINE_BYTES);
42 pool_get_aligned (rm->contexts[thread_index], ctx, CLIB_CACHE_LINE_BYTES);
43 ctx_id = ctx - rm->contexts[thread_index];
45 /* Save state from vlib buffer */
46 ctx->saved_free_list_index = vlib_buffer_get_free_list_index (b0);
47 ctx->current_data = b0->current_data;
48 ctx->flags = b0->flags & VNET_BUFFER_FLAGS_VLAN_BITS;
50 /* Set up vlib buffer hooks */
51 b0->recycle_count = ctx_id;
52 vlib_buffer_set_free_list_index (b0, rm->recycle_list_index);
53 b0->flags |= VLIB_BUFFER_RECYCLE;
55 /* Save feature state */
56 ctx->recycle_node_index = recycle_node_index;
59 clib_memcpy (ctx->vnet_buffer, vnet_buffer (b0),
60 sizeof (vnet_buffer_opaque_t));
62 /* Save packet contents */
63 ctx->l2_packet = l2_packet;
64 ip = (ip4_header_t *) vlib_buffer_get_current (b0);
67 /* Save ethernet header */
68 ctx->l2_header[0] = ((u64 *) ip)[0];
69 ctx->l2_header[1] = ((u64 *) ip)[1];
70 ctx->l2_header[2] = ((u64 *) ip)[2];
71 /* set ip to the true ip header */
72 ip = (ip4_header_t *) (((u8 *) ip) + vnet_buffer (b0)->l2.l2_len);
77 * We need to save TOS for ip4 and ip6 packets.
78 * Fortunately the TOS field is
79 * in the first two bytes of both the ip4 and ip6 headers.
81 ctx->ip_tos = *((u16 *) (ip));
84 * Save the ip4 checksum as well. We just blindly save the corresponding two
85 * bytes even for ip6 packets.
87 ctx->ip4_checksum = ip->checksum;
93 replication_context_t *
94 replication_recycle (vlib_main_t * vm, vlib_buffer_t * b0, u32 is_last)
96 replication_main_t *rm = &replication_main;
97 replication_context_t *ctx;
98 uword thread_index = vm->thread_index;
101 /* Get access to the replication context */
102 ctx = pool_elt_at_index (rm->contexts[thread_index], b0->recycle_count);
104 /* Restore vnet buffer state */
105 clib_memcpy (vnet_buffer (b0), ctx->vnet_buffer,
106 sizeof (vnet_buffer_opaque_t));
108 /* Restore the vlan flags */
109 b0->flags &= ~VNET_BUFFER_FLAGS_VLAN_BITS;
110 b0->flags |= ctx->flags;
112 /* Restore the packet start (current_data) and length */
113 vlib_buffer_advance (b0, ctx->current_data - b0->current_data);
115 /* Restore packet contents */
116 ip = (ip4_header_t *) vlib_buffer_get_current (b0);
119 /* Restore ethernet header */
120 ((u64 *) ip)[0] = ctx->l2_header[0];
121 ((u64 *) ip)[1] = ctx->l2_header[1];
122 ((u64 *) ip)[2] = ctx->l2_header[2];
123 /* set ip to the true ip header */
124 ip = (ip4_header_t *) (((u8 *) ip) + vnet_buffer (b0)->l2.l2_len);
128 *((u16 *) (ip)) = ctx->ip_tos;
129 ip->checksum = ctx->ip4_checksum;
134 * This is the last replication in the list.
135 * Restore original buffer free functionality.
137 vlib_buffer_set_free_list_index (b0, ctx->saved_free_list_index);
138 b0->flags &= ~VLIB_BUFFER_RECYCLE;
140 /* Free context back to its pool */
141 pool_put (rm->contexts[thread_index], ctx);
150 * fish pkts back from the recycle queue/freelist
151 * un-flatten the context chains
154 replication_recycle_callback (vlib_main_t * vm, vlib_buffer_free_list_t * fl)
158 u32 n_left_to_next = 0;
159 u32 n_this_frame = 0;
165 replication_main_t *rm = &replication_main;
166 replication_context_t *ctx;
167 u32 feature_node_index = 0;
168 uword thread_index = vm->thread_index;
171 * All buffers in the list are destined to the same recycle node.
172 * Pull the recycle node index from the first buffer.
173 * Note: this could be sped up if the node index were stuffed into
174 * the freelist itself.
176 if (vec_len (fl->buffers) > 0)
178 bi0 = fl->buffers[0];
179 b0 = vlib_get_buffer (vm, bi0);
180 ctx = pool_elt_at_index (rm->contexts[thread_index], b0->recycle_count);
181 feature_node_index = ctx->recycle_node_index;
185 for (i = 0; i < 2; i++)
190 n_left_from = vec_len (from);
193 while (n_left_from > 0)
195 if (PREDICT_FALSE (n_left_to_next == 0))
199 f->n_vectors = n_this_frame;
200 vlib_put_frame_to_node (vm, feature_node_index, f);
203 f = vlib_get_frame_to_node (vm, feature_node_index);
204 to_next = vlib_frame_vector_args (f);
205 n_left_to_next = VLIB_FRAME_SIZE;
210 if (PREDICT_TRUE (n_left_from > 1))
213 vlib_prefetch_buffer_with_index (vm, pi0, LOAD);
216 b0 = vlib_get_buffer (vm, bi0);
218 /* Mark that this buffer was just recycled */
219 b0->flags |= VLIB_BUFFER_IS_RECYCLED;
222 if (buffer_main.callbacks_registered == 0)
223 vlib_buffer_set_known_state (bi0, VLIB_BUFFER_KNOWN_ALLOCATED);
226 /* If buffer is traced, mark frame as traced */
227 if (PREDICT_FALSE (b0->flags & VLIB_BUFFER_IS_TRACED))
228 f->flags |= VLIB_FRAME_TRACE;
240 vec_reset_length (fl->buffers);
244 ASSERT (n_this_frame);
245 f->n_vectors = n_this_frame;
246 vlib_put_frame_to_node (vm, feature_node_index, f);
251 replication_init (vlib_main_t * vm)
253 replication_main_t *rm = &replication_main;
254 vlib_buffer_free_list_t *fl;
255 __attribute__ ((unused)) replication_context_t *ctx;
256 vlib_thread_main_t *tm = vlib_get_thread_main ();
259 rm->vnet_main = vnet_get_main ();
260 rm->recycle_list_index =
261 vlib_buffer_create_free_list (vm, 1024 /* fictional */ ,
262 "replication-recycle");
264 fl = pool_elt_at_index (vm->buffer_free_list_pool, rm->recycle_list_index);
266 fl->buffers_added_to_freelist_function = replication_recycle_callback;
268 /* Verify the replication context is the expected size */
269 ASSERT (sizeof (replication_context_t) == 128); /* 2 cache lines */
271 vec_validate (rm->contexts, tm->n_vlib_mains - 1);
275 VLIB_INIT_FUNCTION (replication_init);
278 * fd.io coding-style-patch-verification: ON
281 * eval: (c-set-style "gnu")