net/softnic: add command interface
[dpdk.git] / drivers / net / softnic / rte_eth_softnic_internals.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Intel Corporation
3  */
4
5 #ifndef __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__
6 #define __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__
7
8 #include <stddef.h>
9 #include <stdint.h>
10 #include <sys/queue.h>
11
12 #include <rte_mempool.h>
13 #include <rte_mbuf.h>
14 #include <rte_ring.h>
15 #include <rte_ethdev.h>
16 #include <rte_sched.h>
17 #include <rte_port_in_action.h>
18 #include <rte_table_action.h>
19 #include <rte_pipeline.h>
20
21 #include <rte_ethdev_driver.h>
22 #include <rte_tm_driver.h>
23
24 #include "rte_eth_softnic.h"
25
26 #define NAME_SIZE                                            64
27
28 /**
29  * PMD Parameters
30  */
31
32 struct pmd_params {
33         const char *name;
34         const char *firmware;
35         uint32_t cpu_id;
36
37         /** Traffic Management (TM) */
38         struct {
39                 uint32_t n_queues; /**< Number of queues */
40                 uint16_t qsize[RTE_SCHED_TRAFFIC_CLASSES_PER_PIPE];
41         } tm;
42 };
43
44 /**
45  * MEMPOOL
46  */
47 struct softnic_mempool_params {
48         uint32_t buffer_size;
49         uint32_t pool_size;
50         uint32_t cache_size;
51 };
52
53 struct softnic_mempool {
54         TAILQ_ENTRY(softnic_mempool) node;
55         char name[NAME_SIZE];
56         struct rte_mempool *m;
57         uint32_t buffer_size;
58 };
59
60 TAILQ_HEAD(softnic_mempool_list, softnic_mempool);
61
62 /**
63  * SWQ
64  */
65 struct softnic_swq_params {
66         uint32_t size;
67 };
68
69 struct softnic_swq {
70         TAILQ_ENTRY(softnic_swq) node;
71         char name[NAME_SIZE];
72         struct rte_ring *r;
73 };
74
75 TAILQ_HEAD(softnic_swq_list, softnic_swq);
76
77 /**
78  * LINK
79  */
80 struct softnic_link_params {
81         const char *dev_name;
82         uint16_t port_id; /**< Valid only when *dev_name* is NULL. */
83 };
84
85 struct softnic_link {
86         TAILQ_ENTRY(softnic_link) node;
87         char name[NAME_SIZE];
88         uint16_t port_id;
89         uint32_t n_rxq;
90         uint32_t n_txq;
91 };
92
93 TAILQ_HEAD(softnic_link_list, softnic_link);
94
95 /**
96  * TMGR
97  */
98
99 #ifndef TM_MAX_SUBPORTS
100 #define TM_MAX_SUBPORTS                                 8
101 #endif
102
103 #ifndef TM_MAX_PIPES_PER_SUBPORT
104 #define TM_MAX_PIPES_PER_SUBPORT                        4096
105 #endif
106
107 struct tm_params {
108         struct rte_sched_port_params port_params;
109
110         struct rte_sched_subport_params subport_params[TM_MAX_SUBPORTS];
111
112         struct rte_sched_pipe_params
113                 pipe_profiles[RTE_SCHED_PIPE_PROFILES_PER_PORT];
114         uint32_t n_pipe_profiles;
115         uint32_t pipe_to_profile[TM_MAX_SUBPORTS * TM_MAX_PIPES_PER_SUBPORT];
116 };
117
118 /* TM Levels */
119 enum tm_node_level {
120         TM_NODE_LEVEL_PORT = 0,
121         TM_NODE_LEVEL_SUBPORT,
122         TM_NODE_LEVEL_PIPE,
123         TM_NODE_LEVEL_TC,
124         TM_NODE_LEVEL_QUEUE,
125         TM_NODE_LEVEL_MAX,
126 };
127
128 /* TM Shaper Profile */
129 struct tm_shaper_profile {
130         TAILQ_ENTRY(tm_shaper_profile) node;
131         uint32_t shaper_profile_id;
132         uint32_t n_users;
133         struct rte_tm_shaper_params params;
134 };
135
136 TAILQ_HEAD(tm_shaper_profile_list, tm_shaper_profile);
137
138 /* TM Shared Shaper */
139 struct tm_shared_shaper {
140         TAILQ_ENTRY(tm_shared_shaper) node;
141         uint32_t shared_shaper_id;
142         uint32_t n_users;
143         uint32_t shaper_profile_id;
144 };
145
146 TAILQ_HEAD(tm_shared_shaper_list, tm_shared_shaper);
147
148 /* TM WRED Profile */
149 struct tm_wred_profile {
150         TAILQ_ENTRY(tm_wred_profile) node;
151         uint32_t wred_profile_id;
152         uint32_t n_users;
153         struct rte_tm_wred_params params;
154 };
155
156 TAILQ_HEAD(tm_wred_profile_list, tm_wred_profile);
157
158 /* TM Node */
159 struct tm_node {
160         TAILQ_ENTRY(tm_node) node;
161         uint32_t node_id;
162         uint32_t parent_node_id;
163         uint32_t priority;
164         uint32_t weight;
165         uint32_t level;
166         struct tm_node *parent_node;
167         struct tm_shaper_profile *shaper_profile;
168         struct tm_wred_profile *wred_profile;
169         struct rte_tm_node_params params;
170         struct rte_tm_node_stats stats;
171         uint32_t n_children;
172 };
173
174 TAILQ_HEAD(tm_node_list, tm_node);
175
176 /* TM Hierarchy Specification */
177 struct tm_hierarchy {
178         struct tm_shaper_profile_list shaper_profiles;
179         struct tm_shared_shaper_list shared_shapers;
180         struct tm_wred_profile_list wred_profiles;
181         struct tm_node_list nodes;
182
183         uint32_t n_shaper_profiles;
184         uint32_t n_shared_shapers;
185         uint32_t n_wred_profiles;
186         uint32_t n_nodes;
187
188         uint32_t n_tm_nodes[TM_NODE_LEVEL_MAX];
189 };
190
191 struct tm_internals {
192         /** Hierarchy specification
193          *
194          *     -Hierarchy is unfrozen at init and when port is stopped.
195          *     -Hierarchy is frozen on successful hierarchy commit.
196          *     -Run-time hierarchy changes are not allowed, therefore it makes
197          *      sense to keep the hierarchy frozen after the port is started.
198          */
199         struct tm_hierarchy h;
200         int hierarchy_frozen;
201
202         /** Blueprints */
203         struct tm_params params;
204         struct rte_sched_port *sched;
205 };
206
207 struct softnic_tmgr_port {
208         TAILQ_ENTRY(softnic_tmgr_port) node;
209         char name[NAME_SIZE];
210         struct rte_sched_port *s;
211 };
212
213 TAILQ_HEAD(softnic_tmgr_port_list, softnic_tmgr_port);
214
215 /**
216  * TAP
217  */
218 struct softnic_tap {
219         TAILQ_ENTRY(softnic_tap) node;
220         char name[NAME_SIZE];
221         int fd;
222 };
223
224 TAILQ_HEAD(softnic_tap_list, softnic_tap);
225
226 /**
227  * Input port action
228  */
229 struct softnic_port_in_action_profile_params {
230         uint64_t action_mask;
231         struct rte_port_in_action_fltr_config fltr;
232         struct rte_port_in_action_lb_config lb;
233 };
234
235 struct softnic_port_in_action_profile {
236         TAILQ_ENTRY(softnic_port_in_action_profile) node;
237         char name[NAME_SIZE];
238         struct softnic_port_in_action_profile_params params;
239         struct rte_port_in_action_profile *ap;
240 };
241
242 TAILQ_HEAD(softnic_port_in_action_profile_list, softnic_port_in_action_profile);
243
244 /**
245  * Table action
246  */
247 struct softnic_table_action_profile_params {
248         uint64_t action_mask;
249         struct rte_table_action_common_config common;
250         struct rte_table_action_lb_config lb;
251         struct rte_table_action_mtr_config mtr;
252         struct rte_table_action_tm_config tm;
253         struct rte_table_action_encap_config encap;
254         struct rte_table_action_nat_config nat;
255         struct rte_table_action_ttl_config ttl;
256         struct rte_table_action_stats_config stats;
257 };
258
259 struct softnic_table_action_profile {
260         TAILQ_ENTRY(softnic_table_action_profile) node;
261         char name[NAME_SIZE];
262         struct softnic_table_action_profile_params params;
263         struct rte_table_action_profile *ap;
264 };
265
266 TAILQ_HEAD(softnic_table_action_profile_list, softnic_table_action_profile);
267
268 /**
269  * Pipeline
270  */
271 struct pipeline_params {
272         uint32_t timer_period_ms;
273         uint32_t offset_port_id;
274 };
275
276 enum softnic_port_in_type {
277         PORT_IN_RXQ,
278         PORT_IN_SWQ,
279         PORT_IN_TMGR,
280         PORT_IN_TAP,
281         PORT_IN_SOURCE,
282 };
283
284 struct softnic_port_in_params {
285         /* Read */
286         enum softnic_port_in_type type;
287         const char *dev_name;
288         union {
289                 struct {
290                         uint16_t queue_id;
291                 } rxq;
292
293                 struct {
294                         const char *mempool_name;
295                         uint32_t mtu;
296                 } tap;
297
298                 struct {
299                         const char *mempool_name;
300                         const char *file_name;
301                         uint32_t n_bytes_per_pkt;
302                 } source;
303         };
304         uint32_t burst_size;
305
306         /* Action */
307         const char *action_profile_name;
308 };
309
310 enum softnic_port_out_type {
311         PORT_OUT_TXQ,
312         PORT_OUT_SWQ,
313         PORT_OUT_TMGR,
314         PORT_OUT_TAP,
315         PORT_OUT_SINK,
316 };
317
318 struct softnic_port_out_params {
319         enum softnic_port_out_type type;
320         const char *dev_name;
321         union {
322                 struct {
323                         uint16_t queue_id;
324                 } txq;
325
326                 struct {
327                         const char *file_name;
328                         uint32_t max_n_pkts;
329                 } sink;
330         };
331         uint32_t burst_size;
332         int retry;
333         uint32_t n_retries;
334 };
335
336 enum softnic_table_type {
337         TABLE_ACL,
338         TABLE_ARRAY,
339         TABLE_HASH,
340         TABLE_LPM,
341         TABLE_STUB,
342 };
343
344 struct softnic_table_acl_params {
345         uint32_t n_rules;
346         uint32_t ip_header_offset;
347         int ip_version;
348 };
349
350 struct softnic_table_array_params {
351         uint32_t n_keys;
352         uint32_t key_offset;
353 };
354
355 struct softnic_table_hash_params {
356         uint32_t n_keys;
357         uint32_t key_offset;
358         uint32_t key_size;
359         uint8_t *key_mask;
360         uint32_t n_buckets;
361         int extendable_bucket;
362 };
363
364 struct softnic_table_lpm_params {
365         uint32_t n_rules;
366         uint32_t key_offset;
367         uint32_t key_size;
368 };
369
370 struct softnic_table_params {
371         /* Match */
372         enum softnic_table_type match_type;
373         union {
374                 struct softnic_table_acl_params acl;
375                 struct softnic_table_array_params array;
376                 struct softnic_table_hash_params hash;
377                 struct softnic_table_lpm_params lpm;
378         } match;
379
380         /* Action */
381         const char *action_profile_name;
382 };
383
384 struct softnic_port_in {
385         struct softnic_port_in_params params;
386         struct softnic_port_in_action_profile *ap;
387         struct rte_port_in_action *a;
388 };
389
390 struct softnic_table {
391         struct softnic_table_params params;
392         struct softnic_table_action_profile *ap;
393         struct rte_table_action *a;
394 };
395
396 struct pipeline {
397         TAILQ_ENTRY(pipeline) node;
398         char name[NAME_SIZE];
399
400         struct rte_pipeline *p;
401         struct softnic_port_in port_in[RTE_PIPELINE_PORT_IN_MAX];
402         struct softnic_table table[RTE_PIPELINE_TABLE_MAX];
403         uint32_t n_ports_in;
404         uint32_t n_ports_out;
405         uint32_t n_tables;
406
407         struct rte_ring *msgq_req;
408         struct rte_ring *msgq_rsp;
409         uint32_t timer_period_ms;
410
411         int enabled;
412         uint32_t thread_id;
413         uint32_t cpu_id;
414 };
415
416 TAILQ_HEAD(pipeline_list, pipeline);
417
418 /**
419  * Thread
420  */
421 #ifndef THREAD_PIPELINES_MAX
422 #define THREAD_PIPELINES_MAX                               256
423 #endif
424
425 #ifndef THREAD_MSGQ_SIZE
426 #define THREAD_MSGQ_SIZE                                   64
427 #endif
428
429 #ifndef THREAD_TIMER_PERIOD_MS
430 #define THREAD_TIMER_PERIOD_MS                             100
431 #endif
432
433 /**
434  * Master thead: data plane thread context
435  */
436 struct softnic_thread {
437         struct rte_ring *msgq_req;
438         struct rte_ring *msgq_rsp;
439
440         uint32_t enabled;
441 };
442
443 /**
444  * Data plane threads: context
445  */
446 #ifndef TABLE_RULE_ACTION_SIZE_MAX
447 #define TABLE_RULE_ACTION_SIZE_MAX                         2048
448 #endif
449
450 struct softnic_table_data {
451         struct rte_table_action *a;
452 };
453
454 struct pipeline_data {
455         struct rte_pipeline *p;
456         struct softnic_table_data table_data[RTE_PIPELINE_TABLE_MAX];
457         uint32_t n_tables;
458
459         struct rte_ring *msgq_req;
460         struct rte_ring *msgq_rsp;
461         uint64_t timer_period; /* Measured in CPU cycles. */
462         uint64_t time_next;
463
464         uint8_t buffer[TABLE_RULE_ACTION_SIZE_MAX];
465 };
466
467 struct softnic_thread_data {
468         struct rte_pipeline *p[THREAD_PIPELINES_MAX];
469         uint32_t n_pipelines;
470
471         struct pipeline_data pipeline_data[THREAD_PIPELINES_MAX];
472         struct rte_ring *msgq_req;
473         struct rte_ring *msgq_rsp;
474         uint64_t timer_period; /* Measured in CPU cycles. */
475         uint64_t time_next;
476         uint64_t time_next_min;
477         uint64_t iter;
478 } __rte_cache_aligned;
479
480 /**
481  * PMD Internals
482  */
483 struct pmd_internals {
484         /** Params */
485         struct pmd_params params;
486
487         struct {
488                 struct tm_internals tm; /**< Traffic Management */
489         } soft;
490
491         struct softnic_mempool_list mempool_list;
492         struct softnic_swq_list swq_list;
493         struct softnic_link_list link_list;
494         struct softnic_tmgr_port_list tmgr_port_list;
495         struct softnic_tap_list tap_list;
496         struct softnic_port_in_action_profile_list port_in_action_profile_list;
497         struct softnic_table_action_profile_list table_action_profile_list;
498         struct pipeline_list pipeline_list;
499         struct softnic_thread thread[RTE_MAX_LCORE];
500         struct softnic_thread_data thread_data[RTE_MAX_LCORE];
501 };
502
503 /**
504  * MEMPOOL
505  */
506 int
507 softnic_mempool_init(struct pmd_internals *p);
508
509 void
510 softnic_mempool_free(struct pmd_internals *p);
511
512 struct softnic_mempool *
513 softnic_mempool_find(struct pmd_internals *p,
514         const char *name);
515
516 struct softnic_mempool *
517 softnic_mempool_create(struct pmd_internals *p,
518         const char *name,
519         struct softnic_mempool_params *params);
520
521 /**
522  * SWQ
523  */
524 int
525 softnic_swq_init(struct pmd_internals *p);
526
527 void
528 softnic_swq_free(struct pmd_internals *p);
529
530 struct softnic_swq *
531 softnic_swq_find(struct pmd_internals *p,
532         const char *name);
533
534 struct softnic_swq *
535 softnic_swq_create(struct pmd_internals *p,
536         const char *name,
537         struct softnic_swq_params *params);
538
539 /**
540  * LINK
541  */
542 int
543 softnic_link_init(struct pmd_internals *p);
544
545 void
546 softnic_link_free(struct pmd_internals *p);
547
548 struct softnic_link *
549 softnic_link_find(struct pmd_internals *p,
550         const char *name);
551
552 struct softnic_link *
553 softnic_link_create(struct pmd_internals *p,
554         const char *name,
555         struct softnic_link_params *params);
556
557 /**
558  * TMGR
559  */
560 int
561 softnic_tmgr_init(struct pmd_internals *p);
562
563 void
564 softnic_tmgr_free(struct pmd_internals *p);
565
566 struct softnic_tmgr_port *
567 softnic_tmgr_port_find(struct pmd_internals *p,
568         const char *name);
569
570 struct softnic_tmgr_port *
571 softnic_tmgr_port_create(struct pmd_internals *p,
572         const char *name,
573         struct rte_sched_port *sched);
574
575 int
576 tm_init(struct pmd_internals *p);
577
578 void
579 tm_free(struct pmd_internals *p);
580
581 int
582 tm_start(struct pmd_internals *p);
583
584 void
585 tm_stop(struct pmd_internals *p);
586
587 static inline int
588 tm_used(struct rte_eth_dev *dev)
589 {
590         struct pmd_internals *p = dev->data->dev_private;
591
592         return p->soft.tm.h.n_tm_nodes[TM_NODE_LEVEL_PORT];
593 }
594
595 extern const struct rte_tm_ops pmd_tm_ops;
596
597 /**
598  * TAP
599  */
600 int
601 softnic_tap_init(struct pmd_internals *p);
602
603 void
604 softnic_tap_free(struct pmd_internals *p);
605
606 struct softnic_tap *
607 softnic_tap_find(struct pmd_internals *p,
608         const char *name);
609
610 struct softnic_tap *
611 softnic_tap_create(struct pmd_internals *p,
612         const char *name);
613
614 /**
615  * Input port action
616  */
617 int
618 softnic_port_in_action_profile_init(struct pmd_internals *p);
619
620 void
621 softnic_port_in_action_profile_free(struct pmd_internals *p);
622
623 struct softnic_port_in_action_profile *
624 softnic_port_in_action_profile_find(struct pmd_internals *p,
625         const char *name);
626
627 struct softnic_port_in_action_profile *
628 softnic_port_in_action_profile_create(struct pmd_internals *p,
629         const char *name,
630         struct softnic_port_in_action_profile_params *params);
631
632 /**
633  * Table action
634  */
635 int
636 softnic_table_action_profile_init(struct pmd_internals *p);
637
638 void
639 softnic_table_action_profile_free(struct pmd_internals *p);
640
641 struct softnic_table_action_profile *
642 softnic_table_action_profile_find(struct pmd_internals *p,
643         const char *name);
644
645 struct softnic_table_action_profile *
646 softnic_table_action_profile_create(struct pmd_internals *p,
647         const char *name,
648         struct softnic_table_action_profile_params *params);
649
650 /**
651  * Pipeline
652  */
653 int
654 softnic_pipeline_init(struct pmd_internals *p);
655
656 void
657 softnic_pipeline_free(struct pmd_internals *p);
658
659 struct pipeline *
660 softnic_pipeline_find(struct pmd_internals *p, const char *name);
661
662 struct pipeline *
663 softnic_pipeline_create(struct pmd_internals *p,
664         const char *name,
665         struct pipeline_params *params);
666
667 int
668 softnic_pipeline_port_in_create(struct pmd_internals *p,
669         const char *pipeline_name,
670         struct softnic_port_in_params *params,
671         int enabled);
672
673 int
674 softnic_pipeline_port_in_connect_to_table(struct pmd_internals *p,
675         const char *pipeline_name,
676         uint32_t port_id,
677         uint32_t table_id);
678
679 int
680 softnic_pipeline_port_out_create(struct pmd_internals *p,
681         const char *pipeline_name,
682         struct softnic_port_out_params *params);
683
684 int
685 softnic_pipeline_table_create(struct pmd_internals *p,
686         const char *pipeline_name,
687         struct softnic_table_params *params);
688
689 /**
690  * Thread
691  */
692 int
693 softnic_thread_init(struct pmd_internals *p);
694
695 void
696 softnic_thread_free(struct pmd_internals *p);
697
698 /**
699  * CLI
700  */
701 void
702 softnic_cli_process(char *in,
703         char *out,
704         size_t out_size,
705         void *arg);
706
707 int
708 softnic_cli_script_process(struct pmd_internals *softnic,
709         const char *file_name,
710         size_t msg_in_len_max,
711         size_t msg_out_len_max);
712
713 #endif /* __INCLUDE_RTE_ETH_SOFTNIC_INTERNALS_H__ */