NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_switch_control_pipe_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <string.h>
27 #include <unistd.h>
28 
29 #include <rte_byteorder.h>
30 
31 #include <doca_flow.h>
32 #include <doca_log.h>
33 
34 #include "flow_common.h"
35 #include "flow_switch_common.h"
36 
37 DOCA_LOG_REGISTER(FLOW_SWITCH_CONTROL_PIPE);
38 
39 #define NB_ENTRIES 2 /* number of entries in the created control pipe */
40 
41 static struct doca_flow_pipe_entry *entries[NB_ENTRIES]; /* array for storing created entries */
42 
43 /*
44  * Create DOCA Flow control pipe
45  *
46  * @port [in]: port of the pipe
47  * @pipe [out]: created pipe pointer
48  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
49  */
50 static doca_error_t create_control_pipe(struct doca_flow_port *port, struct doca_flow_pipe **pipe)
51 {
52  struct doca_flow_pipe_cfg *pipe_cfg;
54 
55  result = doca_flow_pipe_cfg_create(&pipe_cfg, port);
56  if (result != DOCA_SUCCESS) {
57  DOCA_LOG_ERR("Failed to create doca_flow_pipe_cfg: %s", doca_error_get_descr(result));
58  return result;
59  }
60 
61  result = set_flow_pipe_cfg(pipe_cfg, "CONTROL_PIPE", DOCA_FLOW_PIPE_CONTROL, true);
62  if (result != DOCA_SUCCESS) {
63  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg: %s", doca_error_get_descr(result));
64  goto destroy_pipe_cfg;
65  }
66 
67  result = doca_flow_pipe_create(pipe_cfg, NULL, NULL, pipe);
70  return result;
71 }
72 
73 /*
74  * Add DOCA Flow pipe entries to the control pipe
75  *
76  * @control_pipe [in]: pipe of the entry
77  * @status [in]: user context for adding entry
78  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
79  */
80 static doca_error_t add_control_pipe_entries(struct doca_flow_pipe *control_pipe, struct entries_status *status)
81 {
82  struct doca_flow_match match;
84  struct doca_flow_fwd fwd;
85  uint8_t priority = 0;
86  int entry_index = 0;
88 
89  for (entry_index = 0; entry_index < NB_ENTRIES; entry_index++) {
90  memset(&match, 0, sizeof(match));
91  memset(&monitor, 0, sizeof(monitor));
92  memset(&fwd, 0, sizeof(fwd));
93 
95 
98  match.outer.transport.src_port = rte_cpu_to_be_16(1234 + entry_index);
99  match.outer.transport.dst_port = rte_cpu_to_be_16(80);
100 
102  fwd.port_id = entry_index + 1; /* The port to forward to is defined based on the entry index */
103 
105  priority,
106  control_pipe,
107  &match,
108  NULL,
109  NULL,
110  NULL,
111  NULL,
112  NULL,
113  &monitor,
114  &fwd,
115  status,
116  &entries[entry_index]);
117 
118  if (result != DOCA_SUCCESS) {
119  DOCA_LOG_ERR("Failed to add control pipe entry: %s", doca_error_get_descr(result));
120  return result;
121  }
122  }
123 
124  return DOCA_SUCCESS;
125 }
126 
127 /*
128  * Run flow_switch_control_pipe sample
129  *
130  * @nb_queues [in]: number of queues the sample will use
131  * @ctx [in]: flow switch context the sample will use
132  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
133  */
135 {
136  int nb_ports = 3;
137  struct flow_resources resource = {0};
138  uint32_t nr_shared_resources[SHARED_RESOURCE_NUM_VALUES] = {0};
139  struct doca_flow_port *ports[nb_ports];
140  struct doca_dev *dev_arr[nb_ports];
141  uint32_t actions_mem_size[nb_ports];
142  struct doca_flow_port *switch_port;
143  struct doca_flow_pipe *control_pipe;
144  struct doca_flow_resource_query query_stats;
145  struct entries_status status;
146  int num_of_entries = 2;
148  int entry_idx;
149 
150  memset(&status, 0, sizeof(status));
151  resource.nr_counters = NB_ENTRIES; /* counter per entry */
152 
153  result = init_doca_flow(nb_queues, "switch,hws", &resource, nr_shared_resources);
154  if (result != DOCA_SUCCESS) {
155  DOCA_LOG_ERR("Failed to init DOCA Flow: %s", doca_error_get_descr(result));
156  return result;
157  }
158 
159  memset(dev_arr, 0, sizeof(struct doca_dev *) * nb_ports);
160  dev_arr[0] = ctx->doca_dev[0];
161  ARRAY_INIT(actions_mem_size, ACTIONS_MEM_SIZE(nb_queues, num_of_entries));
163  if (result != DOCA_SUCCESS) {
164  DOCA_LOG_ERR("Failed to init DOCA ports: %s", doca_error_get_descr(result));
166  return result;
167  }
168 
169  switch_port = doca_flow_port_switch_get(NULL);
170 
171  result = create_control_pipe(switch_port, &control_pipe);
172  if (result != DOCA_SUCCESS) {
173  DOCA_LOG_ERR("Failed to create control pipe: %s", doca_error_get_descr(result));
176  return result;
177  }
178 
179  result = add_control_pipe_entries(control_pipe, &status);
180  if (result != DOCA_SUCCESS) {
183  return result;
184  }
185 
186  result = doca_flow_entries_process(switch_port, 0, DEFAULT_TIMEOUT_US, num_of_entries);
187  if (result != DOCA_SUCCESS) {
188  DOCA_LOG_ERR("Failed to process entries: %s", doca_error_get_descr(result));
191  return result;
192  }
193 
194  if (status.nb_processed != num_of_entries || status.failure) {
195  DOCA_LOG_ERR("Failed to process entries");
198  return DOCA_ERROR_BAD_STATE;
199  }
200 
201  DOCA_LOG_INFO("Wait few seconds for packets to arrive");
202  sleep(15);
203 
204  /* dump entries counters */
205  for (entry_idx = 0; entry_idx < NB_ENTRIES; entry_idx++) {
207  if (result != DOCA_SUCCESS) {
208  DOCA_LOG_ERR("Failed to query entry: %s", doca_error_get_descr(result));
211  return result;
212  }
213  DOCA_LOG_INFO("Entry in index: %d", entry_idx);
214  DOCA_LOG_INFO("Total bytes: %ld", query_stats.counter.total_bytes);
215  DOCA_LOG_INFO("Total packets: %ld", query_stats.counter.total_pkts);
216  }
217 
220  return result;
221 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
struct doca_flow_port * init_doca_flow(uint16_t port_id, uint8_t rxq_num)
Definition: flow.c:37
static doca_error_t destroy_pipe_cfg(struct doca_flow_pipe_cfg *cfg)
static struct doca_flow_monitor monitor
Definition: flow_parser.c:108
static struct doca_flow_fwd fwd
Definition: flow_parser.c:109
#define DEFAULT_TIMEOUT_US
Definition: flow_skeleton.c:36
doca_error_t flow_switch_control_pipe(int nb_queues, struct flow_switch_ctx *ctx)
static doca_error_t create_control_pipe(struct doca_flow_port *port, struct doca_flow_pipe **pipe)
static struct doca_flow_pipe_entry * entries[NB_ENTRIES]
static doca_error_t add_control_pipe_entries(struct doca_flow_pipe *control_pipe, struct entries_status *status)
DOCA_LOG_REGISTER(FLOW_SWITCH_CONTROL_PIPE)
static uint8_t entry_idx
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_BAD_STATE
Definition: doca_error.h:56
@ DOCA_SUCCESS
Definition: doca_error.h:38
@ DOCA_FLOW_L4_TYPE_EXT_TRANSPORT
DOCA_STABLE doca_error_t doca_flow_pipe_cfg_destroy(struct doca_flow_pipe_cfg *cfg)
Destroy DOCA Flow pipe configuration struct.
DOCA_STABLE doca_error_t doca_flow_pipe_cfg_create(struct doca_flow_pipe_cfg **cfg, struct doca_flow_port *port)
Create DOCA Flow pipe configuration struct.
DOCA_STABLE doca_error_t doca_flow_entries_process(struct doca_flow_port *port, uint16_t pipe_queue, uint64_t timeout, uint32_t max_processed_entries)
Process entries in queue.
DOCA_EXPERIMENTAL doca_error_t doca_flow_pipe_control_add_entry(uint16_t pipe_queue, uint32_t priority, struct doca_flow_pipe *pipe, const struct doca_flow_match *match, const struct doca_flow_match *match_mask, const struct doca_flow_match_condition *condition, const struct doca_flow_actions *actions, const struct doca_flow_actions *actions_mask, const struct doca_flow_action_descs *action_descs, const struct doca_flow_monitor *monitor, const struct doca_flow_fwd *fwd, void *usr_ctx, struct doca_flow_pipe_entry **entry)
Add one new entry to a control pipe.
DOCA_EXPERIMENTAL doca_error_t doca_flow_pipe_create(const struct doca_flow_pipe_cfg *cfg, const struct doca_flow_fwd *fwd, const struct doca_flow_fwd *fwd_miss, struct doca_flow_pipe **pipe)
Create one new pipe.
DOCA_STABLE void doca_flow_destroy(void)
Destroy the doca flow.
DOCA_STABLE struct doca_flow_port * doca_flow_port_switch_get(const struct doca_flow_port *port)
Get doca flow switch port.
DOCA_EXPERIMENTAL doca_error_t doca_flow_resource_query_entry(struct doca_flow_pipe_entry *entry, struct doca_flow_resource_query *query_stats)
Extract information about specific entry.
@ DOCA_FLOW_PIPE_CONTROL
Definition: doca_flow.h:223
@ DOCA_FLOW_L3_META_IPV4
Definition: doca_flow.h:296
@ DOCA_FLOW_RESOURCE_TYPE_NON_SHARED
Definition: doca_flow.h:615
@ DOCA_FLOW_FWD_PORT
Definition: doca_flow.h:744
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
doca_error_t stop_doca_flow_ports(int nb_ports, struct doca_flow_port *ports[])
Definition: flow_common.c:240
doca_error_t init_doca_flow_ports(int nb_ports, struct doca_flow_port *ports[], bool is_hairpin, struct doca_dev *dev_arr[], uint32_t actions_mem_size[])
Definition: flow_common.c:296
doca_error_t set_flow_pipe_cfg(struct doca_flow_pipe_cfg *cfg, const char *name, enum doca_flow_pipe_type type, bool is_root)
Definition: flow_common.c:305
#define SHARED_RESOURCE_NUM_VALUES
Definition: flow_common.h:59
#define ACTIONS_MEM_SIZE(nr_queues, entries)
Definition: flow_common.h:66
#define ARRAY_INIT(array, val)
Definition: flow_common.h:71
forwarding configuration
Definition: doca_flow.h:779
uint16_t port_id
Definition: doca_flow.h:795
enum doca_flow_fwd_type type
Definition: doca_flow.h:780
enum doca_flow_l4_type_ext l4_type_ext
Definition: doca_flow.h:454
struct doca_flow_header_l4_port transport
Definition: doca_flow.h:463
doca flow matcher information
Definition: doca_flow.h:491
struct doca_flow_parser_meta parser_meta
Definition: doca_flow.h:496
struct doca_flow_header_format outer
Definition: doca_flow.h:498
doca monitor action configuration
Definition: doca_flow.h:968
enum doca_flow_resource_type counter_type
Definition: doca_flow.h:988
enum doca_flow_l3_meta outer_l3_type
Definition: doca_flow.h:382
flow resource query
Definition: doca_flow.h:1101
struct doca_flow_resource_query::@115::@117 counter
user context struct that will be used in entries process callback
Definition: flow_common.h:78
uint32_t nr_counters
Definition: flow_common.h:96
static int nb_ports
Definition: switch_core.c:44
static uint32_t actions_mem_size[FLOW_SWITCH_PORTS_MAX]
Definition: switch_core.c:43
static struct doca_flow_port * ports[FLOW_SWITCH_PORTS_MAX]
Definition: switch_core.c:42
struct upf_accel_ctx * ctx