examples/ip_pipeline: rework flow classification pipeline
[dpdk.git] / examples / ip_pipeline / pipeline / pipeline_flow_classification_be.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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 <string.h>
35
36 #include <rte_common.h>
37 #include <rte_malloc.h>
38 #include <rte_table_hash.h>
39 #include <rte_byteorder.h>
40
41 #include "pipeline_flow_classification_be.h"
42 #include "hash_func.h"
43
44 struct pipeline_flow_classification {
45         struct pipeline p;
46         pipeline_msg_req_handler custom_handlers[PIPELINE_FC_MSG_REQS];
47
48         uint32_t n_flows;
49         uint32_t key_offset;
50         uint32_t key_size;
51         uint32_t hash_offset;
52 } __rte_cache_aligned;
53
54 static void *
55 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg);
56
57 static pipeline_msg_req_handler handlers[] = {
58         [PIPELINE_MSG_REQ_PING] =
59                 pipeline_msg_req_ping_handler,
60         [PIPELINE_MSG_REQ_STATS_PORT_IN] =
61                 pipeline_msg_req_stats_port_in_handler,
62         [PIPELINE_MSG_REQ_STATS_PORT_OUT] =
63                 pipeline_msg_req_stats_port_out_handler,
64         [PIPELINE_MSG_REQ_STATS_TABLE] =
65                 pipeline_msg_req_stats_table_handler,
66         [PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
67                 pipeline_msg_req_port_in_enable_handler,
68         [PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
69                 pipeline_msg_req_port_in_disable_handler,
70         [PIPELINE_MSG_REQ_CUSTOM] =
71                 pipeline_fc_msg_req_custom_handler,
72 };
73
74 static void *
75 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg);
76
77 static void *
78 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg);
79
80 static void *
81 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg);
82
83 static void *
84 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg);
85
86 static void *
87 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg);
88
89 static pipeline_msg_req_handler custom_handlers[] = {
90         [PIPELINE_FC_MSG_REQ_FLOW_ADD] =
91                 pipeline_fc_msg_req_add_handler,
92         [PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK] =
93                 pipeline_fc_msg_req_add_bulk_handler,
94         [PIPELINE_FC_MSG_REQ_FLOW_DEL] =
95                 pipeline_fc_msg_req_del_handler,
96         [PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT] =
97                 pipeline_fc_msg_req_add_default_handler,
98         [PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT] =
99                 pipeline_fc_msg_req_del_default_handler,
100 };
101
102 /*
103  * Flow table
104  */
105 struct flow_table_entry {
106         struct rte_pipeline_table_entry head;
107 };
108
109 rte_table_hash_op_hash hash_func[] = {
110         hash_default_key8,
111         hash_default_key16,
112         hash_default_key24,
113         hash_default_key32,
114         hash_default_key40,
115         hash_default_key48,
116         hash_default_key56,
117         hash_default_key64
118 };
119
120 static int
121 pipeline_fc_parse_args(struct pipeline_flow_classification *p,
122         struct pipeline_params *params)
123 {
124         uint32_t n_flows_present = 0;
125         uint32_t key_offset_present = 0;
126         uint32_t key_size_present = 0;
127         uint32_t hash_offset_present = 0;
128
129         uint32_t i;
130
131         for (i = 0; i < params->n_args; i++) {
132                 char *arg_name = params->args_name[i];
133                 char *arg_value = params->args_value[i];
134
135                 /* n_flows */
136                 if (strcmp(arg_name, "n_flows") == 0) {
137                         if (n_flows_present)
138                                 return -1;
139                         n_flows_present = 1;
140
141                         p->n_flows = atoi(arg_value);
142                         if (p->n_flows == 0)
143                                 return -1;
144
145                         continue;
146                 }
147
148                 /* key_offset */
149                 if (strcmp(arg_name, "key_offset") == 0) {
150                         if (key_offset_present)
151                                 return -1;
152                         key_offset_present = 1;
153
154                         p->key_offset = atoi(arg_value);
155
156                         continue;
157                 }
158
159                 /* key_size */
160                 if (strcmp(arg_name, "key_size") == 0) {
161                         if (key_size_present)
162                                 return -1;
163                         key_size_present = 1;
164
165                         p->key_size = atoi(arg_value);
166                         if ((p->key_size == 0) ||
167                                 (p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
168                                 (p->key_size % 8))
169                                 return -1;
170
171                         continue;
172                 }
173
174                 /* hash_offset */
175                 if (strcmp(arg_name, "hash_offset") == 0) {
176                         if (hash_offset_present)
177                                 return -1;
178                         hash_offset_present = 1;
179
180                         p->hash_offset = atoi(arg_value);
181
182                         continue;
183                 }
184
185                 /* Unknown argument */
186                 return -1;
187         }
188
189         /* Check that mandatory arguments are present */
190         if ((n_flows_present == 0) ||
191                 (key_offset_present == 0) ||
192                 (key_size_present == 0) ||
193                 (hash_offset_present == 0))
194                 return -1;
195
196         return 0;
197 }
198
199 static void *pipeline_fc_init(struct pipeline_params *params,
200         __rte_unused void *arg)
201 {
202         struct pipeline *p;
203         struct pipeline_flow_classification *p_fc;
204         uint32_t size, i;
205
206         /* Check input arguments */
207         if (params == NULL)
208                 return NULL;
209
210         /* Memory allocation */
211         size = RTE_CACHE_LINE_ROUNDUP(
212                 sizeof(struct pipeline_flow_classification));
213         p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
214         if (p == NULL)
215                 return NULL;
216         p_fc = (struct pipeline_flow_classification *) p;
217
218         strcpy(p->name, params->name);
219         p->log_level = params->log_level;
220
221         PLOG(p, HIGH, "Flow classification");
222
223         /* Parse arguments */
224         if (pipeline_fc_parse_args(p_fc, params))
225                 return NULL;
226
227         /* Pipeline */
228         {
229                 struct rte_pipeline_params pipeline_params = {
230                         .name = params->name,
231                         .socket_id = params->socket_id,
232                         .offset_port_id = 0,
233                 };
234
235                 p->p = rte_pipeline_create(&pipeline_params);
236                 if (p->p == NULL) {
237                         rte_free(p);
238                         return NULL;
239                 }
240         }
241
242         /* Input ports */
243         p->n_ports_in = params->n_ports_in;
244         for (i = 0; i < p->n_ports_in; i++) {
245                 struct rte_pipeline_port_in_params port_params = {
246                         .ops = pipeline_port_in_params_get_ops(
247                                 &params->port_in[i]),
248                         .arg_create = pipeline_port_in_params_convert(
249                                 &params->port_in[i]),
250                         .f_action = NULL,
251                         .arg_ah = NULL,
252                         .burst_size = params->port_in[i].burst_size,
253                 };
254
255                 int status = rte_pipeline_port_in_create(p->p,
256                         &port_params,
257                         &p->port_in_id[i]);
258
259                 if (status) {
260                         rte_pipeline_free(p->p);
261                         rte_free(p);
262                         return NULL;
263                 }
264         }
265
266         /* Output ports */
267         p->n_ports_out = params->n_ports_out;
268         for (i = 0; i < p->n_ports_out; i++) {
269                 struct rte_pipeline_port_out_params port_params = {
270                         .ops = pipeline_port_out_params_get_ops(
271                                 &params->port_out[i]),
272                         .arg_create = pipeline_port_out_params_convert(
273                                 &params->port_out[i]),
274                         .f_action = NULL,
275                         .f_action_bulk = NULL,
276                         .arg_ah = NULL,
277                 };
278
279                 int status = rte_pipeline_port_out_create(p->p,
280                         &port_params,
281                         &p->port_out_id[i]);
282
283                 if (status) {
284                         rte_pipeline_free(p->p);
285                         rte_free(p);
286                         return NULL;
287                 }
288         }
289
290         /* Tables */
291         p->n_tables = 1;
292         {
293                 struct rte_table_hash_key8_ext_params
294                         table_hash_key8_params = {
295                         .n_entries = p_fc->n_flows,
296                         .n_entries_ext = p_fc->n_flows,
297                         .signature_offset = p_fc->hash_offset,
298                         .key_offset = p_fc->key_offset,
299                         .f_hash = hash_func[(p_fc->key_size / 8) - 1],
300                         .seed = 0,
301                 };
302
303                 struct rte_table_hash_key16_ext_params
304                         table_hash_key16_params = {
305                         .n_entries = p_fc->n_flows,
306                         .n_entries_ext = p_fc->n_flows,
307                         .signature_offset = p_fc->hash_offset,
308                         .key_offset = p_fc->key_offset,
309                         .f_hash = hash_func[(p_fc->key_size / 8) - 1],
310                         .seed = 0,
311                 };
312
313                 struct rte_table_hash_ext_params
314                         table_hash_params = {
315                         .key_size = p_fc->key_size,
316                         .n_keys = p_fc->n_flows,
317                         .n_buckets = p_fc->n_flows / 4,
318                         .n_buckets_ext = p_fc->n_flows / 4,
319                         .f_hash = hash_func[(p_fc->key_size / 8) - 1],
320                         .seed = 0,
321                         .signature_offset = p_fc->hash_offset,
322                         .key_offset = p_fc->key_offset,
323                 };
324
325                 struct rte_pipeline_table_params table_params = {
326                         .ops = NULL, /* set below */
327                         .arg_create = NULL, /* set below */
328                         .f_action_hit = NULL,
329                         .f_action_miss = NULL,
330                         .arg_ah = NULL,
331                         .action_data_size = sizeof(struct flow_table_entry) -
332                                 sizeof(struct rte_pipeline_table_entry),
333                 };
334
335                 int status;
336
337                 switch (p_fc->key_size) {
338                 case 8:
339                         table_params.ops = &rte_table_hash_key8_lru_ops;
340                         table_params.arg_create = &table_hash_key8_params;
341                         break;
342
343                 case 16:
344                         table_params.ops = &rte_table_hash_key16_ext_ops;
345                         table_params.arg_create = &table_hash_key16_params;
346                         break;
347
348                 default:
349                         table_params.ops = &rte_table_hash_ext_ops;
350                         table_params.arg_create = &table_hash_params;
351                 }
352
353                 status = rte_pipeline_table_create(p->p,
354                         &table_params,
355                         &p->table_id[0]);
356
357                 if (status) {
358                         rte_pipeline_free(p->p);
359                         rte_free(p);
360                         return NULL;
361                 }
362         }
363
364         /* Connecting input ports to tables */
365         for (i = 0; i < p->n_ports_in; i++) {
366                 int status = rte_pipeline_port_in_connect_to_table(p->p,
367                         p->port_in_id[i],
368                         p->table_id[0]);
369
370                 if (status) {
371                         rte_pipeline_free(p->p);
372                         rte_free(p);
373                         return NULL;
374                 }
375         }
376
377         /* Enable input ports */
378         for (i = 0; i < p->n_ports_in; i++) {
379                 int status = rte_pipeline_port_in_enable(p->p,
380                         p->port_in_id[i]);
381
382                 if (status) {
383                         rte_pipeline_free(p->p);
384                         rte_free(p);
385                         return NULL;
386                 }
387         }
388
389         /* Check pipeline consistency */
390         if (rte_pipeline_check(p->p) < 0) {
391                 rte_pipeline_free(p->p);
392                 rte_free(p);
393                 return NULL;
394         }
395
396         /* Message queues */
397         p->n_msgq = params->n_msgq;
398         for (i = 0; i < p->n_msgq; i++)
399                 p->msgq_in[i] = params->msgq_in[i];
400         for (i = 0; i < p->n_msgq; i++)
401                 p->msgq_out[i] = params->msgq_out[i];
402
403         /* Message handlers */
404         memcpy(p->handlers, handlers, sizeof(p->handlers));
405         memcpy(p_fc->custom_handlers,
406                 custom_handlers,
407                 sizeof(p_fc->custom_handlers));
408
409         return p;
410 }
411
412 static int
413 pipeline_fc_free(void *pipeline)
414 {
415         struct pipeline *p = (struct pipeline *) pipeline;
416
417         /* Check input arguments */
418         if (p == NULL)
419                 return -1;
420
421         /* Free resources */
422         rte_pipeline_free(p->p);
423         rte_free(p);
424         return 0;
425 }
426
427 static int
428 pipeline_fc_track(void *pipeline,
429         __rte_unused uint32_t port_in,
430         uint32_t *port_out)
431 {
432         struct pipeline *p = (struct pipeline *) pipeline;
433
434         /* Check input arguments */
435         if ((p == NULL) ||
436                 (port_in >= p->n_ports_in) ||
437                 (port_out == NULL))
438                 return -1;
439
440         if (p->n_ports_in == 1) {
441                 *port_out = 0;
442                 return 0;
443         }
444
445         return -1;
446 }
447
448 static int
449 pipeline_fc_timer(void *pipeline)
450 {
451         struct pipeline *p = (struct pipeline *) pipeline;
452
453         pipeline_msg_req_handle(p);
454         rte_pipeline_flush(p->p);
455
456         return 0;
457 }
458
459 static void *
460 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
461 {
462         struct pipeline_flow_classification *p_fc =
463                         (struct pipeline_flow_classification *) p;
464         struct pipeline_custom_msg_req *req = msg;
465         pipeline_msg_req_handler f_handle;
466
467         f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
468                 p_fc->custom_handlers[req->subtype] :
469                 pipeline_msg_req_invalid_handler;
470
471         if (f_handle == NULL)
472                 f_handle = pipeline_msg_req_invalid_handler;
473
474         return f_handle(p, req);
475 }
476
477 static void *
478 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
479 {
480         struct pipeline_fc_add_msg_req *req = msg;
481         struct pipeline_fc_add_msg_rsp *rsp = msg;
482
483         struct flow_table_entry entry = {
484                 .head = {
485                         .action = RTE_PIPELINE_ACTION_PORT,
486                         {.port_id = p->port_out_id[req->port_id]},
487                 },
488         };
489
490         rsp->status = rte_pipeline_table_entry_add(p->p,
491                 p->table_id[0],
492                 &req->key,
493                 (struct rte_pipeline_table_entry *) &entry,
494                 &rsp->key_found,
495                 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
496
497         return rsp;
498 }
499
500 static void *
501 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
502 {
503         struct pipeline_fc_add_bulk_msg_req *req = msg;
504         struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
505         uint32_t i;
506
507         for (i = 0; i < req->n_keys; i++) {
508                 struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
509                 struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
510
511                 struct flow_table_entry entry = {
512                         .head = {
513                                 .action = RTE_PIPELINE_ACTION_PORT,
514                                 {.port_id = p->port_out_id[flow_req->port_id]},
515                         },
516                 };
517
518                 int status = rte_pipeline_table_entry_add(p->p,
519                         p->table_id[0],
520                         &flow_req->key,
521                         (struct rte_pipeline_table_entry *) &entry,
522                         &flow_rsp->key_found,
523                         (struct rte_pipeline_table_entry **)
524                                 &flow_rsp->entry_ptr);
525
526                 if (status)
527                         break;
528         }
529
530         rsp->n_keys = i;
531
532         return rsp;
533 }
534
535 static void *
536 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
537 {
538         struct pipeline_fc_del_msg_req *req = msg;
539         struct pipeline_fc_del_msg_rsp *rsp = msg;
540
541         rsp->status = rte_pipeline_table_entry_delete(p->p,
542                 p->table_id[0],
543                 &req->key,
544                 &rsp->key_found,
545                 NULL);
546
547         return rsp;
548 }
549
550 static void *
551 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
552 {
553         struct pipeline_fc_add_default_msg_req *req = msg;
554         struct pipeline_fc_add_default_msg_rsp *rsp = msg;
555
556         struct flow_table_entry default_entry = {
557                 .head = {
558                         .action = RTE_PIPELINE_ACTION_PORT,
559                         {.port_id = p->port_out_id[req->port_id]},
560                 },
561         };
562
563         rsp->status = rte_pipeline_table_default_entry_add(p->p,
564                 p->table_id[0],
565                 (struct rte_pipeline_table_entry *) &default_entry,
566                 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
567
568         return rsp;
569 }
570
571 static void *
572 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
573 {
574         struct pipeline_fc_del_default_msg_rsp *rsp = msg;
575
576         rsp->status = rte_pipeline_table_default_entry_delete(p->p,
577                 p->table_id[0],
578                 NULL);
579
580         return rsp;
581 }
582
583 struct pipeline_be_ops pipeline_flow_classification_be_ops = {
584         .f_init = pipeline_fc_init,
585         .f_free = pipeline_fc_free,
586         .f_run = NULL,
587         .f_timer = pipeline_fc_timer,
588         .f_track = pipeline_fc_track,
589 };