999e1b12326c077c86438bbb4cec0f9b721a7d15
[vpp.git] / vnet / vnet / replication.c
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
25 replication_main_t replication_main;
26
27
28 replication_context_t *
29 replication_prep (vlib_main_t * vm,
30                   vlib_buffer_t * b0,
31                   u32 recycle_node_index,
32                   u32 l2_packet)
33 {
34   replication_main_t * rm = &replication_main;
35   replication_context_t * ctx;
36   uword cpu_number = vm->cpu_index;
37   ip4_header_t * ip;
38   u32 ctx_id;
39
40   // Allocate a context, reserve context 0
41   if (PREDICT_FALSE(rm->contexts[cpu_number] == 0))
42     pool_get_aligned (rm->contexts[cpu_number], ctx, CLIB_CACHE_LINE_BYTES);
43       
44   pool_get_aligned (rm->contexts[cpu_number], ctx, CLIB_CACHE_LINE_BYTES);
45   ctx_id = ctx - rm->contexts[cpu_number];
46
47   // Save state from vlib buffer
48   ctx->saved_free_list_index = b0->free_list_index;
49   ctx->current_data = b0->current_data;
50
51   // Set up vlib buffer hooks
52   b0->recycle_count = ctx_id;
53   b0->free_list_index = rm->recycle_list_index;
54   b0->flags |= VLIB_BUFFER_RECYCLE;
55
56   // Save feature state
57   ctx->recycle_node_index = recycle_node_index;
58
59   // Save vnet state
60   clib_memcpy (ctx->vnet_buffer, vnet_buffer(b0), sizeof(vnet_buffer_opaque_t));
61
62   // Save packet contents
63   ctx->l2_packet = l2_packet;
64   ip = (ip4_header_t *)vlib_buffer_get_current (b0);
65   if (l2_packet) {
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   }
73
74   // Copy L3 fields. 
75   // We need to save TOS for ip4 and ip6 packets. Fortunately the TOS field is 
76   // in the first two bytes of both the ip4 and ip6 headers.
77   ctx->ip_tos = *((u16 *)(ip));
78
79   // Save the ip4 checksum as well. We just blindly save the corresponding two
80   // bytes even for ip6 packets. 
81   ctx->ip4_checksum = ip->checksum;
82
83   return ctx;
84 }
85
86
87 replication_context_t *
88 replication_recycle (vlib_main_t * vm,
89                      vlib_buffer_t * b0,
90                      u32 is_last)
91 {
92   replication_main_t * rm = &replication_main;
93   replication_context_t * ctx;
94   uword cpu_number = vm->cpu_index;
95   ip4_header_t * ip;
96
97   // Get access to the replication context
98   ctx = pool_elt_at_index (rm->contexts[cpu_number], b0->recycle_count);
99
100   // Restore vnet buffer state
101   clib_memcpy (vnet_buffer(b0), ctx->vnet_buffer, sizeof(vnet_buffer_opaque_t));
102
103   // Restore the packet start (current_data) and length
104   vlib_buffer_advance(b0, ctx->current_data - b0->current_data);
105
106   // Restore packet contents
107   ip = (ip4_header_t *)vlib_buffer_get_current (b0);
108   if (ctx->l2_packet) {
109     // Restore ethernet header
110     ((u64 *)ip)[0] = ctx->l2_header[0];
111     ((u64 *)ip)[1] = ctx->l2_header[1];
112     ((u64 *)ip)[2] = ctx->l2_header[2];
113     // set ip to the true ip header
114     ip = (ip4_header_t *)(((u8 *)ip) + vnet_buffer(b0)->l2.l2_len);
115   }
116
117   // Restore L3 fields
118   *((u16 *)(ip)) = ctx->ip_tos;
119   ip->checksum = ctx->ip4_checksum;
120
121   if (is_last) {
122     // This is the last replication in the list. 
123     // Restore original buffer free functionality.
124     b0->free_list_index = ctx->saved_free_list_index;
125
126     // Free context back to its pool
127     pool_put (rm->contexts[cpu_number], ctx);
128   }
129
130   return ctx;
131 }
132
133
134
135 /*
136  * fish pkts back from the recycle queue/freelist
137  * un-flatten the context chains
138  */
139 static void replication_recycle_callback (vlib_main_t *vm, 
140                                           vlib_buffer_free_list_t * fl)
141 {
142   vlib_frame_t * f = 0;
143   u32 n_left_from;
144   u32 n_left_to_next = 0;
145   u32 n_this_frame = 0;
146   u32 * from;
147   u32 * to_next = 0;
148   u32 bi0, pi0;
149   vlib_buffer_t *b0;
150   int i;
151   replication_main_t * rm = &replication_main;
152   replication_context_t * ctx;
153   u32 feature_node_index = 0; 
154   uword cpu_number = vm->cpu_index;
155
156   // All buffers in the list are destined to the same recycle node.
157   // Pull the recycle node index from the first buffer. 
158   // Note: this could be sped up if the node index were stuffed into
159   // the freelist itself.
160   if (vec_len (fl->aligned_buffers) > 0) {
161     bi0 = fl->aligned_buffers[0];
162     b0 = vlib_get_buffer (vm, bi0);
163     ctx = pool_elt_at_index (rm->contexts[cpu_number],
164                              b0->recycle_count);
165     feature_node_index = ctx->recycle_node_index;
166   } else if (vec_len (fl->unaligned_buffers) > 0) {
167     bi0 = fl->unaligned_buffers[0];
168     b0 = vlib_get_buffer (vm, bi0);
169     ctx = pool_elt_at_index (rm->contexts[cpu_number], b0->recycle_count);
170     feature_node_index = ctx->recycle_node_index;
171   }
172
173   /* aligned, unaligned buffers */
174   for (i = 0; i < 2; i++) 
175     {
176       if (i == 0)
177         {
178           from = fl->aligned_buffers;
179           n_left_from = vec_len (from);
180         }
181       else
182         {
183           from = fl->unaligned_buffers;
184           n_left_from = vec_len (from);
185         }
186     
187       while (n_left_from > 0)
188         {
189           if (PREDICT_FALSE(n_left_to_next == 0)) 
190             {
191               if (f)
192                 {
193                   f->n_vectors = n_this_frame;
194                   vlib_put_frame_to_node (vm, feature_node_index, f);
195                 }
196               
197               f = vlib_get_frame_to_node (vm, feature_node_index);
198               to_next = vlib_frame_vector_args (f);
199               n_left_to_next = VLIB_FRAME_SIZE;
200               n_this_frame = 0;
201             }
202           
203           bi0 = from[0];
204           if (PREDICT_TRUE(n_left_from > 1))
205             {
206               pi0 = from[1];
207               vlib_prefetch_buffer_with_index(vm,pi0,LOAD);
208             }
209
210           b0 = vlib_get_buffer (vm, bi0);
211
212           // Mark that this buffer was just recycled
213           b0->flags |= VLIB_BUFFER_IS_RECYCLED;
214
215           // If buffer is traced, mark frame as traced
216           if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED))
217               f->flags |= VLIB_FRAME_TRACE;
218
219           to_next[0] = bi0;
220
221           from++;
222           to_next++;
223           n_this_frame++;
224           n_left_to_next--;
225           n_left_from--;
226         }
227     }
228   
229   vec_reset_length (fl->aligned_buffers);
230   vec_reset_length (fl->unaligned_buffers);
231
232   if (f)
233     {
234       ASSERT(n_this_frame);
235       f->n_vectors = n_this_frame;
236       vlib_put_frame_to_node (vm, feature_node_index, f);
237     }
238 }
239
240
241
242 clib_error_t *replication_init (vlib_main_t *vm)
243 {
244   replication_main_t * rm = &replication_main;
245   vlib_buffer_main_t * bm = vm->buffer_main;
246   vlib_buffer_free_list_t * fl;
247   __attribute__((unused)) replication_context_t * ctx;
248   vlib_thread_main_t * tm = vlib_get_thread_main();
249     
250   rm->vlib_main = vm;
251   rm->vnet_main = vnet_get_main();
252   rm->recycle_list_index = 
253     vlib_buffer_create_free_list (vm, 1024 /* fictional */, 
254                                   "replication-recycle");
255
256   fl = pool_elt_at_index (bm->buffer_free_list_pool, 
257                           rm->recycle_list_index);
258
259   fl->buffers_added_to_freelist_function = replication_recycle_callback;
260
261   // Verify the replication context is the expected size
262   ASSERT(sizeof(replication_context_t) == 128); // 2 cache lines
263
264   vec_validate (rm->contexts, tm->n_vlib_mains - 1);
265   return 0;
266 }
267
268 VLIB_INIT_FUNCTION (replication_init);