NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_switch_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_log.h>
32 #include <doca_flow.h>
33 
34 #include "flow_common.h"
35 
36 DOCA_LOG_REGISTER(FLOW_SWITCH);
37 
38 #define NB_ENTRIES 2
39 
40 static struct doca_flow_pipe_entry *entries[2 * NB_ENTRIES]; /* array for storing created entries */
41 
42 /*
43  * Create DOCA Flow pipe with 5 tuple match on the switch port.
44  * Matched traffic will be forwarded to the port defined per entry.
45  * Unmatched traffic will be dropped.
46  *
47  * @sw_port [in]: switch port
48  * @pipe [out]: created pipe pointer
49  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
50  */
51 static doca_error_t create_switch_pipe(struct doca_flow_port *sw_port, struct doca_flow_pipe **pipe)
52 {
53  struct doca_flow_match match;
55  struct doca_flow_fwd fwd;
56  struct doca_flow_fwd fwd_miss;
57  struct doca_flow_pipe_cfg *pipe_cfg;
59 
60  memset(&match, 0, sizeof(match));
61  memset(&monitor, 0, sizeof(monitor));
62  memset(&fwd, 0, sizeof(fwd));
63  memset(&fwd_miss, 0, sizeof(fwd_miss));
64 
69 
70  /* Source, destination IP addresses and source, destination TCP ports are defined per entry */
71  match.outer.ip4.src_ip = 0xffffffff;
72  match.outer.ip4.dst_ip = 0xffffffff;
73  match.outer.tcp.l4_port.src_port = 0xffff;
74  match.outer.tcp.l4_port.dst_port = 0xffff;
75 
77 
78  /* Port ID to forward to is defined per entry */
79  fwd.port_id = 0xffff;
80 
81  /* Unmatched packets will be dropped */
83 
85 
86  result = doca_flow_pipe_cfg_create(&pipe_cfg, sw_port);
87  if (result != DOCA_SUCCESS) {
88  DOCA_LOG_ERR("Failed to create doca_flow_pipe_cfg: %s", doca_error_get_descr(result));
89  return result;
90  }
91 
92  result = set_flow_pipe_cfg(pipe_cfg, "SWITCH_PIPE", DOCA_FLOW_PIPE_BASIC, true);
93  if (result != DOCA_SUCCESS) {
94  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg: %s", doca_error_get_descr(result));
95  goto destroy_pipe_cfg;
96  }
98  if (result != DOCA_SUCCESS) {
99  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg nr_entries: %s", doca_error_get_descr(result));
100  goto destroy_pipe_cfg;
101  }
102  result = doca_flow_pipe_cfg_set_match(pipe_cfg, &match, NULL);
103  if (result != DOCA_SUCCESS) {
104  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg match: %s", doca_error_get_descr(result));
105  goto destroy_pipe_cfg;
106  }
108  if (result != DOCA_SUCCESS) {
109  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg monitor: %s", doca_error_get_descr(result));
110  goto destroy_pipe_cfg;
111  }
112 
113  result = doca_flow_pipe_create(pipe_cfg, &fwd, &fwd_miss, pipe);
115  doca_flow_pipe_cfg_destroy(pipe_cfg);
116  return result;
117 }
118 
119 /*
120  * Add DOCA Flow pipe entry to the pipe
121  *
122  * @switch_num [in]: switch number (1, 2 ...)
123  * @pipe [in]: pipe of the entry
124  * @status [in]: user context for adding entry
125  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
126  */
127 static doca_error_t add_switch_pipe_entries(int switch_num, struct doca_flow_pipe *pipe, struct entries_status *status)
128 {
129  struct doca_flow_match match;
130  struct doca_flow_fwd fwd;
133  int entry_index = 0;
134  int port_base;
135  int entry_base = (switch_num - 1) * 2;
136 
137  port_base = (switch_num - 1) * 3;
138  doca_be32_t dst_ip_addr;
139  doca_be32_t src_ip_addr;
142 
143  memset(&fwd, 0, sizeof(fwd));
144  memset(&match, 0, sizeof(match));
145 
146  for (entry_index = 0; entry_index < NB_ENTRIES; entry_index++) {
147  dst_ip_addr = BE_IPV4_ADDR(8, 8, 8, 8 + entry_base + entry_index);
148  src_ip_addr = BE_IPV4_ADDR(1, 2, 3, 4 + entry_base + entry_index);
149  dst_port = rte_cpu_to_be_16(80);
150  src_port = rte_cpu_to_be_16(1234);
151 
152  match.outer.ip4.dst_ip = dst_ip_addr;
153  match.outer.ip4.src_ip = src_ip_addr;
156 
158  fwd.port_id = port_base + 1 + entry_index; /* The port to forward to is defined based on the entry index
159  */
160 
161  /* last entry should be inserted with DOCA_FLOW_NO_WAIT flag */
162  if (entry_index == NB_ENTRIES - 1)
163  flags = DOCA_FLOW_NO_WAIT;
164 
166  pipe,
167  &match,
168  NULL,
169  NULL,
170  &fwd,
171  flags,
172  status,
173  &entries[entry_base + entry_index]);
174 
175  if (result != DOCA_SUCCESS) {
176  DOCA_LOG_ERR("Failed to add pipe entry: %s", doca_error_get_descr(result));
177  return result;
178  }
179  }
180 
181  return DOCA_SUCCESS;
182 }
183 /*
184  * Run flow_switch sample
185  *
186  * @nb_queues [in]: number of queues the sample will use
187  * @nb_ports [in]: number of ports the sample will use
188  * @dev_main [in]: the main doca proxy port
189  * @dev_sec [in]: the second doca proxy port
190  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
191  */
192 doca_error_t flow_switch(int nb_queues, int nb_ports, struct doca_dev *dev_main, struct doca_dev *dev_sec)
193 {
194  struct flow_resources resource = {0};
195  uint32_t nr_shared_resources[SHARED_RESOURCE_NUM_VALUES] = {0};
196  struct doca_flow_port *ports[nb_ports];
197  struct doca_dev *dev_arr[nb_ports];
198  uint32_t actions_mem_size[nb_ports];
199  struct doca_flow_pipe *pipe1;
200  struct doca_flow_pipe *pipe2;
201  struct doca_flow_resource_query query_stats;
202  struct entries_status status;
204  int entry_idx;
205 
206  memset(&status, 0, sizeof(status));
207  resource.nr_counters = 2 * NB_ENTRIES; /* counter per entry */
208 
209  result = init_doca_flow(nb_queues, "switch,hws,isolated", &resource, nr_shared_resources);
210  if (result != DOCA_SUCCESS) {
211  DOCA_LOG_ERR("Failed to init DOCA Flow: %s", doca_error_get_descr(result));
212  return result;
213  }
214 
215  memset(dev_arr, 0, sizeof(struct doca_dev *) * nb_ports);
216  dev_arr[0] = dev_main;
217  dev_arr[3] = dev_sec;
219  result = init_doca_flow_ports(nb_ports, ports, false /* is_hairpin */, dev_arr, actions_mem_size);
220  if (result != DOCA_SUCCESS) {
221  DOCA_LOG_ERR("Failed to init DOCA ports: %s", doca_error_get_descr(result));
223  return result;
224  }
225 
227  if (result != DOCA_SUCCESS) {
228  DOCA_LOG_ERR("Failed to create pipe: %s", doca_error_get_descr(result));
231  return result;
232  }
233 
234  result = add_switch_pipe_entries(1, pipe1, &status);
235  if (result != DOCA_SUCCESS) {
236  DOCA_LOG_ERR("Failed to add entries to the pipe: %s", doca_error_get_descr(result));
239  return result;
240  }
241 
243  if (result != DOCA_SUCCESS) {
244  DOCA_LOG_ERR("Failed to process entries: %s", doca_error_get_descr(result));
247  return result;
248  }
249 
251  if (result != DOCA_SUCCESS) {
252  DOCA_LOG_ERR("Failed to create pipe: %s", doca_error_get_descr(result));
255  return result;
256  }
257 
258  result = add_switch_pipe_entries(2, pipe2, &status);
259  if (result != DOCA_SUCCESS) {
260  DOCA_LOG_ERR("Failed to add entries to the pipe: %s", doca_error_get_descr(result));
263  return result;
264  }
265 
267  if (result != DOCA_SUCCESS) {
268  DOCA_LOG_ERR("Failed to process entries: %s", doca_error_get_descr(result));
271  return result;
272  }
273 
274  if (status.nb_processed != 2 * NB_ENTRIES || status.failure) {
275  DOCA_LOG_ERR("Failed to process entries");
278  return DOCA_ERROR_BAD_STATE;
279  }
280 
281  DOCA_LOG_INFO("Wait few seconds for packets to arrive");
282  sleep(15);
283 
284  /* dump entries counters */
285  for (entry_idx = 0; entry_idx < 2 * NB_ENTRIES; entry_idx++) {
287  if (result != DOCA_SUCCESS) {
288  DOCA_LOG_ERR("Failed to query entry: %s", doca_error_get_descr(result));
291  return result;
292  }
293  DOCA_LOG_INFO("Entry in index: %d", entry_idx);
294  DOCA_LOG_INFO("Total bytes: %ld", query_stats.counter.total_bytes);
295  DOCA_LOG_INFO("Total packets: %ld", query_stats.counter.total_pkts);
296  }
297 
300  return result;
301 }
#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_fwd fwd_miss
Definition: flow_parser.c:110
#define BE_IPV4_ADDR(a, b, c, d)
Definition: flow_parser.c:64
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
static uint8_t entry_idx
DOCA_LOG_REGISTER(FLOW_SWITCH)
static doca_error_t add_switch_pipe_entries(int switch_num, struct doca_flow_pipe *pipe, struct entries_status *status)
#define NB_ENTRIES
doca_error_t flow_switch(int nb_queues, int nb_ports, struct doca_dev *dev_main, struct doca_dev *dev_sec)
static struct doca_flow_pipe_entry * entries[2 *NB_ENTRIES]
static doca_error_t create_switch_pipe(struct doca_flow_port *sw_port, struct doca_flow_pipe **pipe)
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_TCP
@ DOCA_FLOW_L3_TYPE_IP4
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_cfg_set_match(struct doca_flow_pipe_cfg *cfg, const struct doca_flow_match *match, const struct doca_flow_match *match_mask)
Set pipe's match and match mask.
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_flow_flags_type
doca flow flags type
Definition: doca_flow.h:114
DOCA_EXPERIMENTAL doca_error_t doca_flow_pipe_cfg_set_monitor(struct doca_flow_pipe_cfg *cfg, const struct doca_flow_monitor *monitor)
Set pipe's monitor.
DOCA_EXPERIMENTAL doca_error_t doca_flow_pipe_add_entry(uint16_t pipe_queue, struct doca_flow_pipe *pipe, const struct doca_flow_match *match, const struct doca_flow_actions *actions, const struct doca_flow_monitor *monitor, const struct doca_flow_fwd *fwd, uint32_t flags, void *usr_ctx, struct doca_flow_pipe_entry **entry)
Add one new entry to a pipe.
DOCA_STABLE void doca_flow_destroy(void)
Destroy the doca flow.
DOCA_STABLE doca_error_t doca_flow_pipe_cfg_set_nr_entries(struct doca_flow_pipe_cfg *cfg, uint32_t nr_entries)
Set pipe's maximum number of flow rules.
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_BASIC
Definition: doca_flow.h:221
@ DOCA_FLOW_L3_META_IPV4
Definition: doca_flow.h:296
@ DOCA_FLOW_NO_WAIT
Definition: doca_flow.h:115
@ DOCA_FLOW_WAIT_FOR_BATCH
Definition: doca_flow.h:117
@ DOCA_FLOW_RESOURCE_TYPE_NON_SHARED
Definition: doca_flow.h:615
@ DOCA_FLOW_FWD_PORT
Definition: doca_flow.h:744
@ DOCA_FLOW_FWD_DROP
Definition: doca_flow.h:748
@ DOCA_FLOW_L4_META_TCP
Definition: doca_flow.h:308
#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
uint32_t doca_be32_t
Definition: doca_types.h:121
uint16_t doca_be16_t
Declare DOCA endianity types.
Definition: doca_types.h:120
uint16_t src_port
Definition: packets.h:0
uint16_t dst_port
Definition: packets.h:1
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
struct doca_flow_pipe * pipe
Definition: doca_flow.h:806
uint16_t port_id
Definition: doca_flow.h:795
enum doca_flow_fwd_type type
Definition: doca_flow.h:780
struct doca_flow_header_ip4 ip4
Definition: doca_flow.h:449
enum doca_flow_l4_type_ext l4_type_ext
Definition: doca_flow.h:454
enum doca_flow_l3_type l3_type
Definition: doca_flow.h:446
struct doca_flow_header_tcp tcp
Definition: doca_flow.h:461
struct doca_flow_header_l4_port l4_port
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
enum doca_flow_l4_meta outer_l4_type
Definition: doca_flow.h:383
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