68d4f936223860846a455b041007965fa5ab54fd
[dpdk.git] / examples / ip_pipeline / pipeline_flow_classification.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_malloc.h>
39 #include <rte_log.h>
40 #include <rte_ethdev.h>
41 #include <rte_ether.h>
42 #include <rte_ip.h>
43 #include <rte_byteorder.h>
44
45 #include <rte_port_ring.h>
46 #include <rte_table_hash.h>
47 #include <rte_pipeline.h>
48
49 #include "main.h"
50
51 struct app_core_fc_message_handle_params {
52         struct rte_ring *ring_req;
53         struct rte_ring *ring_resp;
54
55         struct rte_pipeline *p;
56         uint32_t *port_out_id;
57         uint32_t table_id;
58 };
59
60 static void
61 app_message_handle(struct app_core_fc_message_handle_params *params);
62
63 static int app_flow_classification_table_init(
64         struct rte_pipeline *p,
65         uint32_t *port_out_id,
66         uint32_t table_id)
67 {
68         struct app_flow_key flow_key;
69         uint32_t i;
70
71         /* Add entries to tables */
72         for (i = 0; i < (1 << 24); i++) {
73                 struct rte_pipeline_table_entry entry = {
74                         .action = RTE_PIPELINE_ACTION_PORT,
75                         {.port_id = port_out_id[i & (app.n_ports - 1)]},
76                 };
77                 struct rte_pipeline_table_entry *entry_ptr;
78                 int key_found, status;
79
80                 flow_key.ttl = 0;
81                 flow_key.proto = 6; /* TCP */
82                 flow_key.header_checksum = 0;
83                 flow_key.ip_src = 0;
84                 flow_key.ip_dst = rte_bswap32(i);
85                 flow_key.port_src = 0;
86                 flow_key.port_dst = 0;
87
88                 status = rte_pipeline_table_entry_add(p, table_id,
89                         (void *) &flow_key, &entry, &key_found, &entry_ptr);
90                 if (status < 0)
91                         rte_panic("Unable to add entry to table %u (%d)\n",
92                                 table_id, status);
93         }
94
95         return 0;
96 }
97
98 void
99 app_main_loop_pipeline_flow_classification(void) {
100         struct rte_pipeline_params pipeline_params = {
101                 .name = "pipeline",
102                 .socket_id = rte_socket_id(),
103         };
104
105         struct rte_pipeline *p;
106         uint32_t port_in_id[APP_MAX_PORTS];
107         uint32_t port_out_id[APP_MAX_PORTS];
108         uint32_t table_id;
109         uint32_t i;
110
111         uint32_t core_id = rte_lcore_id();
112         struct app_core_params *core_params = app_get_core_params(core_id);
113         struct app_core_fc_message_handle_params mh_params;
114
115         if ((core_params == NULL) || (core_params->core_type != APP_CORE_FC))
116                 rte_panic("Core %u misconfiguration\n", core_id);
117
118         RTE_LOG(INFO, USER1, "Core %u is doing flow classification "
119                 "(pipeline with hash table, 16-byte key, LRU)\n", core_id);
120
121         /* Pipeline configuration */
122         p = rte_pipeline_create(&pipeline_params);
123         if (p == NULL)
124                 rte_panic("Unable to configure the pipeline\n");
125
126         /* Input port configuration */
127         for (i = 0; i < app.n_ports; i++) {
128                 struct rte_port_ring_reader_params port_ring_params = {
129                         .ring = app.rings[core_params->swq_in[i]],
130                 };
131
132                 struct rte_pipeline_port_in_params port_params = {
133                         .ops = &rte_port_ring_reader_ops,
134                         .arg_create = (void *) &port_ring_params,
135                         .f_action = NULL,
136                         .arg_ah = NULL,
137                         .burst_size = app.bsz_swq_rd,
138                 };
139
140                 if (rte_pipeline_port_in_create(p, &port_params,
141                         &port_in_id[i]))
142                         rte_panic("Unable to configure input port for "
143                                 "ring %d\n", i);
144         }
145
146         /* Output port configuration */
147         for (i = 0; i < app.n_ports; i++) {
148                 struct rte_port_ring_writer_params port_ring_params = {
149                         .ring = app.rings[core_params->swq_out[i]],
150                         .tx_burst_sz = app.bsz_swq_wr,
151                 };
152
153                 struct rte_pipeline_port_out_params port_params = {
154                         .ops = &rte_port_ring_writer_ops,
155                         .arg_create = (void *) &port_ring_params,
156                         .f_action = NULL,
157                         .f_action_bulk = NULL,
158                         .arg_ah = NULL,
159                 };
160
161                 if (rte_pipeline_port_out_create(p, &port_params,
162                         &port_out_id[i]))
163                         rte_panic("Unable to configure output port for "
164                                 "ring %d\n", i);
165         }
166
167         /* Table configuration */
168         {
169                 struct rte_table_hash_key16_lru_params table_hash_params = {
170                         .n_entries = 1 << 24,
171                         .signature_offset = __builtin_offsetof(
172                                 struct app_pkt_metadata, signature),
173                         .key_offset = __builtin_offsetof(
174                                 struct app_pkt_metadata, flow_key),
175                         .f_hash = test_hash,
176                         .seed = 0,
177                 };
178
179                 struct rte_pipeline_table_params table_params = {
180                         .ops = &rte_table_hash_key16_lru_ops,
181                         .arg_create = &table_hash_params,
182                         .f_action_hit = NULL,
183                         .f_action_miss = NULL,
184                         .arg_ah = NULL,
185                         .action_data_size = 0,
186                 };
187
188                 if (rte_pipeline_table_create(p, &table_params, &table_id))
189                         rte_panic("Unable to configure the hash table\n");
190         }
191
192         /* Interconnecting ports and tables */
193         for (i = 0; i < app.n_ports; i++)
194                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
195                         table_id))
196                         rte_panic("Unable to connect input port %u to "
197                                 "table %u\n", port_in_id[i],  table_id);
198
199         /* Enable input ports */
200         for (i = 0; i < app.n_ports; i++)
201                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
202                         rte_panic("Unable to enable input port %u\n",
203                                 port_in_id[i]);
204
205         /* Check pipeline consistency */
206         if (rte_pipeline_check(p) < 0)
207                 rte_panic("Pipeline consistency check failed\n");
208
209         /* Message handling */
210         mh_params.ring_req = app_get_ring_req(
211                 app_get_first_core_id(APP_CORE_FC));
212         mh_params.ring_resp = app_get_ring_resp(
213                 app_get_first_core_id(APP_CORE_FC));
214         mh_params.p = p;
215         mh_params.port_out_id = port_out_id;
216         mh_params.table_id = table_id;
217
218         /* Run-time */
219         for (i = 0; ; i++) {
220                 rte_pipeline_run(p);
221
222                 if ((i & APP_FLUSH) == 0) {
223                         rte_pipeline_flush(p);
224                         app_message_handle(&mh_params);
225                 }
226         }
227 }
228
229 void
230 app_message_handle(struct app_core_fc_message_handle_params *params)
231 {
232         struct rte_ring *ring_req = params->ring_req;
233         struct rte_ring *ring_resp;
234         void *msg;
235         struct app_msg_req *req;
236         struct app_msg_resp *resp;
237         struct rte_pipeline *p;
238         uint32_t *port_out_id;
239         uint32_t table_id;
240         int result;
241
242         /* Read request message */
243         result = rte_ring_sc_dequeue(ring_req, &msg);
244         if (result != 0)
245                 return;
246
247         ring_resp = params->ring_resp;
248         p = params->p;
249         port_out_id = params->port_out_id;
250         table_id = params->table_id;
251
252         /* Handle request */
253         req = (struct app_msg_req *) ((struct rte_mbuf *)msg)->ctrl.data;
254         switch (req->type) {
255         case APP_MSG_REQ_PING:
256         {
257                 result = 0;
258                 break;
259         }
260
261         case APP_MSG_REQ_FC_ADD_ALL:
262         {
263                 result = app_flow_classification_table_init(p, port_out_id,
264                         table_id);
265                 break;
266         }
267
268         case APP_MSG_REQ_FC_ADD:
269         {
270                 struct rte_pipeline_table_entry entry = {
271                         .action = RTE_PIPELINE_ACTION_PORT,
272                         {.port_id = port_out_id[req->flow_classif_add.port]},
273                 };
274
275                 struct rte_pipeline_table_entry *entry_ptr;
276
277                 int key_found;
278
279                 result = rte_pipeline_table_entry_add(p, table_id,
280                         req->flow_classif_add.key_raw, &entry, &key_found,
281                         &entry_ptr);
282                 break;
283         }
284
285         case APP_MSG_REQ_FC_DEL:
286         {
287                 int key_found;
288
289                 result = rte_pipeline_table_entry_delete(p, table_id,
290                         req->flow_classif_add.key_raw, &key_found, NULL);
291                 break;
292         }
293
294         default:
295                 rte_panic("FC Unrecognized message type (%u)\n", req->type);
296         }
297
298         /* Fill in response message */
299         resp = (struct app_msg_resp *) ((struct rte_mbuf *)msg)->ctrl.data;
300         resp->result = result;
301
302         /* Send response */
303         do {
304                 result = rte_ring_sp_enqueue(ring_resp, msg);
305         } while (result == -ENOBUFS);
306 }