add prefix to cache line macros
[dpdk.git] / examples / ip_pipeline / pipeline_rx.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36
37 #include <rte_common.h>
38 #include <rte_byteorder.h>
39 #include <rte_log.h>
40 #include <rte_malloc.h>
41 #include <rte_ethdev.h>
42 #include <rte_mbuf.h>
43 #include <rte_ether.h>
44 #include <rte_ip.h>
45 #include <rte_jhash.h>
46
47 #include <rte_port_ethdev.h>
48 #include <rte_port_ring.h>
49 #include <rte_table_stub.h>
50 #include <rte_pipeline.h>
51
52
53 #include "main.h"
54
55 struct app_core_rx_message_handle_params {
56         struct rte_ring *ring_req;
57         struct rte_ring *ring_resp;
58
59         struct rte_pipeline *p;
60         uint32_t *port_in_id;
61 };
62
63 static void
64 app_message_handle(struct app_core_rx_message_handle_params *params);
65
66 static int
67 app_pipeline_rx_port_in_action_handler(struct rte_mbuf **pkts, uint32_t n,
68         uint64_t *pkts_mask, void *arg);
69
70 void
71 app_main_loop_pipeline_rx(void) {
72         struct rte_pipeline *p;
73         uint32_t port_in_id[APP_MAX_PORTS];
74         uint32_t port_out_id[APP_MAX_PORTS];
75         uint32_t table_id[APP_MAX_PORTS];
76         uint32_t i;
77
78         uint32_t core_id = rte_lcore_id();
79         struct app_core_params *core_params = app_get_core_params(core_id);
80         struct app_core_rx_message_handle_params mh_params;
81
82         if ((core_params == NULL) || (core_params->core_type != APP_CORE_RX))
83                 rte_panic("Core %u misconfiguration\n", core_id);
84
85         RTE_LOG(INFO, USER1, "Core %u is doing RX\n", core_id);
86
87         /* Pipeline configuration */
88         struct rte_pipeline_params pipeline_params = {
89                 .name = "pipeline",
90                 .socket_id = rte_socket_id(),
91         };
92
93         p = rte_pipeline_create(&pipeline_params);
94         if (p == NULL)
95                 rte_panic("%s: Unable to configure the pipeline\n", __func__);
96
97         /* Input port configuration */
98         for (i = 0; i < app.n_ports; i++) {
99                 struct rte_port_ethdev_reader_params port_ethdev_params = {
100                         .port_id = app.ports[i],
101                         .queue_id = 0,
102                 };
103
104                 struct rte_pipeline_port_in_params port_params = {
105                         .ops = &rte_port_ethdev_reader_ops,
106                         .arg_create = (void *) &port_ethdev_params,
107                         .f_action = app_pipeline_rx_port_in_action_handler,
108                         .arg_ah = NULL,
109                         .burst_size = app.bsz_hwq_rd,
110                 };
111
112                 if (rte_pipeline_port_in_create(p, &port_params,
113                         &port_in_id[i]))
114                         rte_panic("%s: Unable to configure input port for "
115                                 "port %d\n", __func__, app.ports[i]);
116         }
117
118         /* Output port configuration */
119         for (i = 0; i < app.n_ports; i++) {
120                 struct rte_port_ring_writer_params port_ring_params = {
121                         .ring = app.rings[core_params->swq_out[i]],
122                         .tx_burst_sz = app.bsz_swq_wr,
123                 };
124
125                 struct rte_pipeline_port_out_params port_params = {
126                         .ops = &rte_port_ring_writer_ops,
127                         .arg_create = (void *) &port_ring_params,
128                         .f_action = NULL,
129                         .f_action_bulk = NULL,
130                         .arg_ah = NULL,
131                 };
132
133                 if (rte_pipeline_port_out_create(p, &port_params,
134                         &port_out_id[i]))
135                         rte_panic("%s: Unable to configure output port for "
136                                 "ring RX %i\n", __func__, i);
137         }
138
139         /* Table configuration */
140         for (i = 0; i < app.n_ports; i++) {
141                 struct rte_pipeline_table_params table_params = {
142                         .ops = &rte_table_stub_ops,
143                         .arg_create = NULL,
144                         .f_action_hit = NULL,
145                         .f_action_miss = NULL,
146                         .arg_ah = NULL,
147                         .action_data_size = 0,
148                 };
149
150                 if (rte_pipeline_table_create(p, &table_params, &table_id[i]))
151                         rte_panic("%s: Unable to configure table %u\n",
152                                 __func__, table_id[i]);
153         }
154
155         /* Interconnecting ports and tables */
156         for (i = 0; i < app.n_ports; i++)
157                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
158                         table_id[i]))
159                         rte_panic("%s: Unable to connect input port %u to "
160                                 "table %u\n", __func__, port_in_id[i],
161                                 table_id[i]);
162
163         /* Add entries to tables */
164         for (i = 0; i < app.n_ports; i++) {
165                 struct rte_pipeline_table_entry default_entry = {
166                         .action = RTE_PIPELINE_ACTION_PORT,
167                         {.port_id = port_out_id[i]},
168                 };
169
170                 struct rte_pipeline_table_entry *default_entry_ptr;
171
172                 if (rte_pipeline_table_default_entry_add(p, table_id[i],
173                         &default_entry, &default_entry_ptr))
174                         rte_panic("%s: Unable to add default entry to "
175                                 "table %u\n", __func__, table_id[i]);
176         }
177
178         /* Enable input ports */
179         for (i = 0; i < app.n_ports; i++)
180                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
181                         rte_panic("Unable to enable input port %u\n",
182                                 port_in_id[i]);
183
184         /* Check pipeline consistency */
185         if (rte_pipeline_check(p) < 0)
186                 rte_panic("%s: Pipeline consistency check failed\n", __func__);
187
188         /* Message handling */
189         mh_params.ring_req =
190                 app_get_ring_req(app_get_first_core_id(APP_CORE_RX));
191         mh_params.ring_resp =
192                 app_get_ring_resp(app_get_first_core_id(APP_CORE_RX));
193         mh_params.p = p;
194         mh_params.port_in_id = port_in_id;
195
196         /* Run-time */
197         for (i = 0; ; i++) {
198                 rte_pipeline_run(p);
199
200                 if ((i & APP_FLUSH) == 0) {
201                         rte_pipeline_flush(p);
202                         app_message_handle(&mh_params);
203                 }
204         }
205 }
206
207 uint64_t test_hash(
208         void *key,
209         __attribute__((unused)) uint32_t key_size,
210         __attribute__((unused)) uint64_t seed)
211 {
212         struct app_flow_key *flow_key = (struct app_flow_key *) key;
213         uint32_t ip_dst = rte_be_to_cpu_32(flow_key->ip_dst);
214         uint64_t signature = (ip_dst & 0x00FFFFFFLLU) >> 2;
215
216         return signature;
217 }
218
219 uint32_t
220 rte_jhash2_16(uint32_t *k, uint32_t initval)
221 {
222         uint32_t a, b, c;
223
224         a = b = RTE_JHASH_GOLDEN_RATIO;
225         c = initval;
226
227         a += k[0];
228         b += k[1];
229         c += k[2];
230         __rte_jhash_mix(a, b, c);
231
232         c += 16; /* length in bytes */
233         a += k[3]; /* Remaining word */
234
235         __rte_jhash_mix(a, b, c);
236
237         return c;
238 }
239
240 static inline void
241 app_pkt_metadata_fill(struct rte_mbuf *m)
242 {
243         uint8_t *m_data = rte_pktmbuf_mtod(m, uint8_t *);
244         struct app_pkt_metadata *c =
245                 (struct app_pkt_metadata *) RTE_MBUF_METADATA_UINT8_PTR(m, 0);
246         struct ipv4_hdr *ip_hdr =
247                 (struct ipv4_hdr *) &m_data[sizeof(struct ether_hdr)];
248         uint64_t *ipv4_hdr_slab = (uint64_t *) ip_hdr;
249
250         /* TTL and Header Checksum are set to 0 */
251         c->flow_key.slab0 = ipv4_hdr_slab[1] & 0xFFFFFFFF0000FF00LLU;
252         c->flow_key.slab1 = ipv4_hdr_slab[2];
253         c->signature = test_hash((void *) &c->flow_key, 0, 0);
254
255         /* Pop Ethernet header */
256         if (app.ether_hdr_pop_push) {
257                 rte_pktmbuf_adj(m, (uint16_t)sizeof(struct ether_hdr));
258                 m->l2_len = 0;
259                 m->l3_len = sizeof(struct ipv4_hdr);
260         }
261 }
262
263 int
264 app_pipeline_rx_port_in_action_handler(
265         struct rte_mbuf **pkts,
266         uint32_t n,
267         uint64_t *pkts_mask,
268         __rte_unused void *arg)
269 {
270         uint32_t i;
271
272         for (i = 0; i < n; i++) {
273                 struct rte_mbuf *m = pkts[i];
274
275                 app_pkt_metadata_fill(m);
276         }
277
278         *pkts_mask = (~0LLU) >> (64 - n);
279
280         return 0;
281 }
282
283 void
284 app_main_loop_rx(void) {
285         struct app_mbuf_array *ma;
286         uint32_t i, j;
287         int ret;
288
289         uint32_t core_id = rte_lcore_id();
290         struct app_core_params *core_params = app_get_core_params(core_id);
291
292         if ((core_params == NULL) || (core_params->core_type != APP_CORE_RX))
293                 rte_panic("Core %u misconfiguration\n", core_id);
294
295         RTE_LOG(INFO, USER1, "Core %u is doing RX (no pipeline)\n", core_id);
296
297         ma = rte_malloc_socket(NULL, sizeof(struct app_mbuf_array),
298                 RTE_CACHE_LINE_SIZE, rte_socket_id());
299         if (ma == NULL)
300                 rte_panic("%s: cannot allocate buffer space\n", __func__);
301
302         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
303                 uint32_t n_mbufs;
304
305                 n_mbufs = rte_eth_rx_burst(
306                         app.ports[i],
307                         0,
308                         ma->array,
309                         app.bsz_hwq_rd);
310
311                 if (n_mbufs == 0)
312                         continue;
313
314                 for (j = 0; j < n_mbufs; j++) {
315                         struct rte_mbuf *m = ma->array[j];
316
317                         app_pkt_metadata_fill(m);
318                 }
319
320                 do {
321                         ret = rte_ring_sp_enqueue_bulk(
322                                 app.rings[core_params->swq_out[i]],
323                                 (void **) ma->array,
324                                 n_mbufs);
325                 } while (ret < 0);
326         }
327 }
328
329 void
330 app_message_handle(struct app_core_rx_message_handle_params *params)
331 {
332         struct rte_ring *ring_req = params->ring_req;
333         struct rte_ring *ring_resp;
334         void *msg;
335         struct app_msg_req *req;
336         struct app_msg_resp *resp;
337         struct rte_pipeline *p;
338         uint32_t *port_in_id;
339         int result;
340
341         /* Read request message */
342         result = rte_ring_sc_dequeue(ring_req, &msg);
343         if (result != 0)
344                 return;
345
346         ring_resp = params->ring_resp;
347         p = params->p;
348         port_in_id = params->port_in_id;
349
350         /* Handle request */
351         req = (struct app_msg_req *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
352         switch (req->type) {
353         case APP_MSG_REQ_PING:
354         {
355                 result = 0;
356                 break;
357         }
358
359         case APP_MSG_REQ_RX_PORT_ENABLE:
360         {
361                 result = rte_pipeline_port_in_enable(p,
362                         port_in_id[req->rx_up.port]);
363                 break;
364         }
365
366         case APP_MSG_REQ_RX_PORT_DISABLE:
367         {
368                 result = rte_pipeline_port_in_disable(p,
369                         port_in_id[req->rx_down.port]);
370                 break;
371         }
372
373         default:
374                 rte_panic("RX Unrecognized message type (%u)\n", req->type);
375         }
376
377         /* Fill in response message */
378         resp = (struct app_msg_resp *)rte_ctrlmbuf_data((struct rte_mbuf *)msg);
379         resp->result = result;
380
381         /* Send response */
382         do {
383                 result = rte_ring_sp_enqueue(ring_resp, msg);
384         } while (result == -ENOBUFS);
385 }