add prefix to cache line macros
[dpdk.git] / examples / ip_pipeline / pipeline_tx.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 #include <stdint.h>
37
38 #include <rte_common.h>
39 #include <rte_byteorder.h>
40 #include <rte_log.h>
41 #include <rte_malloc.h>
42 #include <rte_ethdev.h>
43 #include <rte_mbuf.h>
44 #include <rte_ether.h>
45 #include <rte_ip.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 #include "main.h"
53
54 static struct ether_addr local_ether_addr = {
55         .addr_bytes = {0, 1, 2, 3, 4, 5},
56 };
57
58 static inline void
59 app_pkt_metadata_flush(struct rte_mbuf *pkt)
60 {
61         struct app_pkt_metadata *pkt_meta = (struct app_pkt_metadata *)
62                 RTE_MBUF_METADATA_UINT8_PTR(pkt, 0);
63         struct ether_hdr *ether_hdr = (struct ether_hdr *)
64                 rte_pktmbuf_prepend(pkt, (uint16_t) sizeof(struct ether_hdr));
65
66         ether_addr_copy(&pkt_meta->nh_arp, &ether_hdr->d_addr);
67         ether_addr_copy(&local_ether_addr, &ether_hdr->s_addr);
68         ether_hdr->ether_type = rte_bswap16(ETHER_TYPE_IPv4);
69         pkt->l2_len = sizeof(struct ether_hdr);
70 }
71
72 static int
73 app_pipeline_tx_port_in_action_handler(
74         struct rte_mbuf **pkts,
75         uint32_t n,
76         uint64_t *pkts_mask,
77         __rte_unused void *arg)
78 {
79         uint32_t i;
80
81         for (i = 0; i < n; i++) {
82                 struct rte_mbuf *m = pkts[i];
83
84                 app_pkt_metadata_flush(m);
85         }
86
87         *pkts_mask = (~0LLU) >> (64 - n);
88
89         return 0;
90 }
91
92 void
93 app_main_loop_pipeline_tx(void) {
94         struct rte_pipeline *p;
95         uint32_t port_in_id[APP_MAX_PORTS];
96         uint32_t port_out_id[APP_MAX_PORTS];
97         uint32_t table_id[APP_MAX_PORTS];
98         uint32_t i;
99
100         uint32_t core_id = rte_lcore_id();
101         struct app_core_params *core_params = app_get_core_params(core_id);
102
103         if ((core_params == NULL) || (core_params->core_type != APP_CORE_TX))
104                 rte_panic("Core %u misconfiguration\n", core_id);
105
106         RTE_LOG(INFO, USER1, "Core %u is doing TX\n", core_id);
107
108         /* Pipeline configuration */
109         struct rte_pipeline_params pipeline_params = {
110                 .name = "pipeline",
111                 .socket_id = rte_socket_id(),
112         };
113
114         p = rte_pipeline_create(&pipeline_params);
115         if (p == NULL)
116                 rte_panic("%s: Unable to configure the pipeline\n", __func__);
117
118         /* Input port configuration */
119         for (i = 0; i < app.n_ports; i++) {
120                 struct rte_port_ring_reader_params port_ring_params = {
121                         .ring = app.rings[core_params->swq_in[i]],
122                 };
123
124                 struct rte_pipeline_port_in_params port_params = {
125                         .ops = &rte_port_ring_reader_ops,
126                         .arg_create = (void *) &port_ring_params,
127                         .f_action = (app.ether_hdr_pop_push) ?
128                                 app_pipeline_tx_port_in_action_handler : NULL,
129                         .arg_ah = NULL,
130                         .burst_size = app.bsz_swq_rd,
131                 };
132
133                 if (rte_pipeline_port_in_create(p, &port_params,
134                         &port_in_id[i])) {
135                         rte_panic("%s: Unable to configure input port for "
136                                 "ring TX %i\n", __func__, i);
137                 }
138         }
139
140         /* Output port configuration */
141         for (i = 0; i < app.n_ports; i++) {
142                 struct rte_port_ethdev_writer_params port_ethdev_params = {
143                         .port_id = app.ports[i],
144                         .queue_id = 0,
145                         .tx_burst_sz = app.bsz_hwq_wr,
146                 };
147
148                 struct rte_pipeline_port_out_params port_params = {
149                         .ops = &rte_port_ethdev_writer_ops,
150                         .arg_create = (void *) &port_ethdev_params,
151                         .f_action = NULL,
152                         .f_action_bulk = NULL,
153                         .arg_ah = NULL,
154                 };
155
156                 if (rte_pipeline_port_out_create(p, &port_params,
157                         &port_out_id[i])) {
158                         rte_panic("%s: Unable to configure output port for "
159                                 "port %d\n", __func__, app.ports[i]);
160                 }
161         }
162
163         /* Table configuration */
164         for (i = 0; i < app.n_ports; i++) {
165                 struct rte_pipeline_table_params table_params = {
166                         .ops = &rte_table_stub_ops,
167                         .arg_create = NULL,
168                         .f_action_hit = NULL,
169                         .f_action_miss = NULL,
170                         .arg_ah = NULL,
171                         .action_data_size = 0,
172                 };
173
174                 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
175                         rte_panic("%s: Unable to configure table %u\n",
176                                 __func__, table_id[i]);
177                 }
178         }
179
180         /* Interconnecting ports and tables */
181         for (i = 0; i < app.n_ports; i++)
182                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
183                         table_id[i]))
184                         rte_panic("%s: Unable to connect input port %u to "
185                                 "table %u\n", __func__, port_in_id[i],
186                                 table_id[i]);
187
188         /* Add entries to tables */
189         for (i = 0; i < app.n_ports; i++) {
190                 struct rte_pipeline_table_entry default_entry = {
191                         .action = RTE_PIPELINE_ACTION_PORT,
192                         {.port_id = port_out_id[i]},
193                 };
194
195                 struct rte_pipeline_table_entry *default_entry_ptr;
196
197                 if (rte_pipeline_table_default_entry_add(p, table_id[i],
198                         &default_entry, &default_entry_ptr))
199                         rte_panic("%s: Unable to add default entry to "
200                                 "table %u\n", __func__, table_id[i]);
201         }
202
203         /* Enable input ports */
204         for (i = 0; i < app.n_ports; i++)
205                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
206                         rte_panic("Unable to enable input port %u\n",
207                                 port_in_id[i]);
208
209         /* Check pipeline consistency */
210         if (rte_pipeline_check(p) < 0)
211                 rte_panic("%s: Pipeline consistency check failed\n", __func__);
212
213         /* Run-time */
214         for (i = 0; ; i++) {
215                 rte_pipeline_run(p);
216
217                 if ((i & APP_FLUSH) == 0)
218                         rte_pipeline_flush(p);
219         }
220 }
221
222 void
223 app_main_loop_tx(void) {
224         struct app_mbuf_array *m[APP_MAX_PORTS];
225         uint32_t i;
226
227         uint32_t core_id = rte_lcore_id();
228         struct app_core_params *core_params = app_get_core_params(core_id);
229
230         if ((core_params == NULL) || (core_params->core_type != APP_CORE_TX))
231                 rte_panic("Core %u misconfiguration\n", core_id);
232
233         RTE_LOG(INFO, USER1, "Core %u is doing TX (no pipeline)\n", core_id);
234
235         for (i = 0; i < APP_MAX_PORTS; i++) {
236                 m[i] = rte_malloc_socket(NULL, sizeof(struct app_mbuf_array),
237                         RTE_CACHE_LINE_SIZE, rte_socket_id());
238                 if (m[i] == NULL)
239                         rte_panic("%s: Cannot allocate buffer space\n",
240                                 __func__);
241         }
242
243         for (i = 0; ; i = ((i + 1) & (app.n_ports - 1))) {
244                 uint32_t n_mbufs, n_pkts;
245                 int ret;
246
247                 n_mbufs = m[i]->n_mbufs;
248
249                 ret = rte_ring_sc_dequeue_bulk(
250                         app.rings[core_params->swq_in[i]],
251                         (void **) &m[i]->array[n_mbufs],
252                         app.bsz_swq_rd);
253
254                 if (ret == -ENOENT)
255                         continue;
256
257                 n_mbufs += app.bsz_swq_rd;
258
259                 if (n_mbufs < app.bsz_hwq_wr) {
260                         m[i]->n_mbufs = n_mbufs;
261                         continue;
262                 }
263
264                 n_pkts = rte_eth_tx_burst(
265                         app.ports[i],
266                         0,
267                         m[i]->array,
268                         n_mbufs);
269
270                 if (n_pkts < n_mbufs) {
271                         uint32_t k;
272
273                         for (k = n_pkts; k < n_mbufs; k++) {
274                                 struct rte_mbuf *pkt_to_free;
275
276                                 pkt_to_free = m[i]->array[k];
277                                 rte_pktmbuf_free(pkt_to_free);
278                         }
279                 }
280
281                 m[i]->n_mbufs = 0;
282         }
283 }