FD.io VPP  v18.07-rc0-415-g6c78436
Vector Packet Processing
replication.c
Go to the documentation of this file.
1 /*
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 
26 
27 
30  vlib_buffer_t * b0, u32 recycle_node_index, u32 l2_packet)
31 {
34  uword thread_index = vm->thread_index;
35  ip4_header_t *ip;
36  u32 ctx_id;
37 
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);
41 
42  pool_get_aligned (rm->contexts[thread_index], ctx, CLIB_CACHE_LINE_BYTES);
43  ctx_id = ctx - rm->contexts[thread_index];
44 
45  /* Save state from vlib buffer */
47  ctx->current_data = b0->current_data;
49 
50  /* Set up vlib buffer hooks */
51  b0->recycle_count = ctx_id;
53  b0->flags |= VLIB_BUFFER_RECYCLE;
54 
55  /* Save feature state */
56  ctx->recycle_node_index = recycle_node_index;
57 
58  /* Save vnet state */
60  sizeof (vnet_buffer_opaque_t));
61 
62  /* Save packet contents */
63  ctx->l2_packet = l2_packet;
65  if (l2_packet)
66  {
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);
73  }
74 
75  /*
76  * Copy L3 fields.
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.
80  */
81  ctx->ip_tos = *((u16 *) (ip));
82 
83  /*
84  * Save the ip4 checksum as well. We just blindly save the corresponding two
85  * bytes even for ip6 packets.
86  */
87  ctx->ip4_checksum = ip->checksum;
88 
89  return ctx;
90 }
91 
92 
95 {
98  uword thread_index = vm->thread_index;
99  ip4_header_t *ip;
100 
101  /* Get access to the replication context */
102  ctx = pool_elt_at_index (rm->contexts[thread_index], b0->recycle_count);
103 
104  /* Restore vnet buffer state */
105  clib_memcpy (vnet_buffer (b0), ctx->vnet_buffer,
106  sizeof (vnet_buffer_opaque_t));
107 
108  /* Restore the vlan flags */
110  b0->flags |= ctx->flags;
111 
112  /* Restore the packet start (current_data) and length */
114 
115  /* Restore packet contents */
117  if (ctx->l2_packet)
118  {
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);
125  }
126 
127  // Restore L3 fields
128  *((u16 *) (ip)) = ctx->ip_tos;
129  ip->checksum = ctx->ip4_checksum;
130 
131  if (is_last)
132  {
133  /*
134  * This is the last replication in the list.
135  * Restore original buffer free functionality.
136  */
138  b0->flags &= ~VLIB_BUFFER_RECYCLE;
139 
140  /* Free context back to its pool */
141  pool_put (rm->contexts[thread_index], ctx);
142  }
143 
144  return ctx;
145 }
146 
147 
148 
149 /*
150  * fish pkts back from the recycle queue/freelist
151  * un-flatten the context chains
152  */
153 static void
155 {
156  vlib_frame_t *f = 0;
157  u32 n_left_from;
158  u32 n_left_to_next = 0;
159  u32 n_this_frame = 0;
160  u32 *from;
161  u32 *to_next = 0;
162  u32 bi0, pi0;
163  vlib_buffer_t *b0;
164  int i;
167  u32 feature_node_index = 0;
168  uword thread_index = vm->thread_index;
169 
170  /*
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.
175  */
176  if (vec_len (fl->buffers) > 0)
177  {
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;
182  }
183 
184  /* buffers */
185  for (i = 0; i < 2; i++)
186  {
187  if (i == 0)
188  {
189  from = fl->buffers;
190  n_left_from = vec_len (from);
191  }
192 
193  while (n_left_from > 0)
194  {
195  if (PREDICT_FALSE (n_left_to_next == 0))
196  {
197  if (f)
198  {
199  f->n_vectors = n_this_frame;
200  vlib_put_frame_to_node (vm, feature_node_index, f);
201  }
202 
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;
206  n_this_frame = 0;
207  }
208 
209  bi0 = from[0];
210  if (PREDICT_TRUE (n_left_from > 1))
211  {
212  pi0 = from[1];
213  vlib_prefetch_buffer_with_index (vm, pi0, LOAD);
214  }
215 
216  b0 = vlib_get_buffer (vm, bi0);
217 
218  /* Mark that this buffer was just recycled */
219  b0->flags |= VLIB_BUFFER_IS_RECYCLED;
220 
221 #if (CLIB_DEBUG > 0)
224 #endif
225 
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;
229 
230  to_next[0] = bi0;
231 
232  from++;
233  to_next++;
234  n_this_frame++;
235  n_left_to_next--;
236  n_left_from--;
237  }
238  }
239 
241 
242  if (f)
243  {
244  ASSERT (n_this_frame);
245  f->n_vectors = n_this_frame;
246  vlib_put_frame_to_node (vm, feature_node_index, f);
247  }
248 }
249 
250 clib_error_t *
252 {
255  __attribute__ ((unused)) replication_context_t *ctx;
257 
258  rm->vlib_main = vm;
259  rm->vnet_main = vnet_get_main ();
260  rm->recycle_list_index =
261  vlib_buffer_create_free_list (vm, 1024 /* fictional */ ,
262  "replication-recycle");
263 
265 
267 
268  /* Verify the replication context is the expected size */
269  ASSERT (sizeof (replication_context_t) == 128); /* 2 cache lines */
270 
271  vec_validate (rm->contexts, tm->n_vlib_mains - 1);
272  return 0;
273 }
274 
276 
277 /*
278  * fd.io coding-style-patch-verification: ON
279  *
280  * Local Variables:
281  * eval: (c-set-style "gnu")
282  * End:
283  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:437
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
#define PREDICT_TRUE(x)
Definition: clib.h:106
unsigned long u64
Definition: types.h:89
u32 thread_index
Definition: main.h:176
int i
u32 recycle_count
Used by L2 path recycle code.
Definition: buffer.h:150
unsigned char u8
Definition: types.h:56
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
replication_main_t replication_main
Definition: replication.c:25
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:104
#define vlib_prefetch_buffer_with_index(vm, bi, type)
Prefetch buffer metadata by buffer index The first 64 bytes of buffer contains most header informatio...
Definition: buffer_funcs.h:324
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:156
vlib_frame_t * vlib_get_frame_to_node(vlib_main_t *vm, u32 to_node_index)
Definition: main.c:182
unsigned int u32
Definition: types.h:88
#define VLIB_FRAME_SIZE
Definition: node.h:364
#define fl(x, y)
int callbacks_registered
Definition: buffer.h:455
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:461
unsigned short u16
Definition: types.h:57
void vlib_put_frame_to_node(vlib_main_t *vm, u32 to_node_index, vlib_frame_t *f)
Definition: main.c:191
vlib_buffer_free_list_index_t saved_free_list_index
Definition: replication.h:42
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:202
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:273
#define PREDICT_FALSE(x)
Definition: clib.h:105
static void replication_recycle_callback(vlib_main_t *vm, vlib_buffer_free_list_t *fl)
Definition: replication.c:154
replication_context_t * replication_prep(vlib_main_t *vm, vlib_buffer_t *b0, u32 recycle_node_index, u32 l2_packet)
Definition: replication.c:29
vlib_buffer_free_list_t * buffer_free_list_pool
Definition: main.h:108
static void vlib_buffer_set_known_state(u32 buffer_index, vlib_buffer_known_state_t state)
Definition: buffer_funcs.h:379
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:188
u16 n_vectors
Definition: node.h:380
vlib_main_t * vm
Definition: buffer.c:294
#define clib_memcpy(a, b, c)
Definition: string.h:75
void(* buffers_added_to_freelist_function)(struct vlib_main_t *vm, struct vlib_buffer_free_list_t *fl)
Definition: buffer.h:376
vlib_main_t * vlib_main
Definition: replication.h:66
#define ASSERT(truth)
long ctx[MAX_CONNS]
Definition: main.c:126
static vlib_buffer_free_list_index_t vlib_buffer_get_free_list_index(vlib_buffer_t *b)
Definition: buffer_funcs.h:401
u16 flags
Definition: node.h:371
vnet_main_t * vnet_main
Definition: replication.h:67
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:215
clib_error_t * replication_init(vlib_main_t *vm)
Definition: replication.c:251
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:267
#define VLIB_FRAME_TRACE
Definition: node.h:415
replication_context_t * replication_recycle(vlib_main_t *vm, vlib_buffer_t *b0, u32 is_last)
Definition: replication.c:94
#define vnet_buffer(b)
Definition: buffer.h:360
static vlib_thread_main_t * vlib_get_thread_main()
Definition: global_funcs.h:32
vlib_buffer_main_t buffer_main
Definition: buffer.c:52
static void vlib_buffer_set_free_list_index(vlib_buffer_t *b, vlib_buffer_free_list_index_t index)
Definition: buffer_funcs.h:410
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:62
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:111
#define VNET_BUFFER_FLAGS_VLAN_BITS
Definition: buffer.h:69
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
vlib_buffer_free_list_index_t vlib_buffer_create_free_list(vlib_main_t *vm, u32 n_data_bytes, char *fmt,...)
Definition: buffer.c:415
replication_context_t ** contexts
Definition: replication.h:64