NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_multi_fwd_sample.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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 <doca_log.h>
30 #include <doca_flow.h>
31 
32 #include "flow_common.h"
33 
34 DOCA_LOG_REGISTER(FLOW_MULTI_FWD);
35 
36 /*
37  * Create DOCA Flow control pipe
38  *
39  * @port [in]: port of the pipe
40  * @pipe [out]: created pipe pointer
41  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
42  */
43 doca_error_t create_multi_fwd_pipe(struct doca_flow_port *port, struct doca_flow_pipe **pipe)
44 {
45  struct doca_flow_match match;
46  struct doca_flow_actions actions, *actions_arr[NB_ACTIONS_ARR];
47  struct doca_flow_pipe_cfg *pipe_cfg;
50 
51  memset(&match, 0, sizeof(match));
52  memset(&actions, 0, sizeof(actions));
53 
54  /* match on outer source IP address */
57  match.outer.ip4.src_ip = 0xffffffff;
58 
59  actions_arr[0] = &actions;
60 
61  result = doca_flow_pipe_cfg_create(&pipe_cfg, port);
62  if (result != DOCA_SUCCESS) {
63  DOCA_LOG_ERR("Failed to create doca_flow_pipe_cfg: %s", doca_error_get_descr(result));
64  return result;
65  }
66 
67  result = set_flow_pipe_cfg(pipe_cfg, "HAIRPIN_PIPE", DOCA_FLOW_PIPE_BASIC, true);
68  if (result != DOCA_SUCCESS) {
69  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg: %s", doca_error_get_descr(result));
70  goto destroy_pipe_cfg;
71  }
72  result = doca_flow_pipe_cfg_set_match(pipe_cfg, &match, NULL);
73  if (result != DOCA_SUCCESS) {
74  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg match: %s", doca_error_get_descr(result));
75  goto destroy_pipe_cfg;
76  }
77  result = doca_flow_pipe_cfg_set_actions(pipe_cfg, actions_arr, NULL, NULL, NB_ACTIONS_ARR);
78  if (result != DOCA_SUCCESS) {
79  DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg actions: %s", doca_error_get_descr(result));
80  goto destroy_pipe_cfg;
81  }
82 
83  /* set fwd type = DOCA_FLOW_FWD_CHANGEABLE for choosing different forward per entry */
84  result = doca_flow_pipe_create(pipe_cfg, &fwd, NULL, pipe);
87  return result;
88 }
89 
90 /*
91  * Add DOCA Flow pipe entries to the LPM pipe. one entry with full mask and one with 16 bits mask
92  *
93  * @pipe [in]: pipe of the entry
94  * @port_id [in]: port ID of the entry
95  * @status [in]: user context for adding entry
96  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
97  */
98 doca_error_t add_multi_fwd_pipe_entries(struct doca_flow_pipe *pipe, int port_id, struct entries_status *status)
99 {
100  struct doca_flow_fwd fwd;
101  struct doca_flow_match match;
102  struct doca_flow_actions actions;
103  struct doca_flow_pipe_entry *entry0, *entry1;
105  doca_be32_t src_ip_addr;
106 
107  memset(&fwd, 0, sizeof(fwd));
108  memset(&match, 0, sizeof(match));
109  memset(&actions, 0, sizeof(actions));
110 
111  src_ip_addr = BE_IPV4_ADDR(1, 1, 1, 1);
112  match.outer.ip4.src_ip = src_ip_addr;
113 
114  /* add the first entry with fwd port */
116  fwd.port_id = port_id ^ 1;
117 
118  result = doca_flow_pipe_add_entry(0, pipe, &match, &actions, NULL, &fwd, 0, status, &entry0);
119  if (result != DOCA_SUCCESS) {
120  DOCA_LOG_ERR("Failed to add first entry: %s", doca_error_get_descr(result));
121  return result;
122  }
123 
124  src_ip_addr = BE_IPV4_ADDR(2, 2, 2, 2);
125  match.outer.ip4.src_ip = src_ip_addr;
126 
127  /* add the second entry with fwd drop */
129 
130  result = doca_flow_pipe_add_entry(0, pipe, &match, &actions, NULL, &fwd, 0, status, &entry1);
131  if (result != DOCA_SUCCESS) {
132  DOCA_LOG_ERR("Failed to add second entry: %s", doca_error_get_descr(result));
133  return result;
134  }
135 
136  return DOCA_SUCCESS;
137 }
138 
139 /*
140  * Run flow_multi_fwd sample
141  *
142  * @nb_queues [in]: number of queues the sample will use
143  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
144  */
146 {
147  int nb_ports = 2;
148  struct flow_resources resource = {0};
149  uint32_t nr_shared_resources[SHARED_RESOURCE_NUM_VALUES] = {0};
150  struct doca_flow_port *ports[nb_ports];
151  struct doca_dev *dev_arr[nb_ports];
152  uint32_t actions_mem_size[nb_ports];
153  struct doca_flow_pipe *pipe;
154  int port_id;
155  struct entries_status status;
156  int num_of_entries = 2;
158 
159  result = init_doca_flow(nb_queues, "vnf,hws", &resource, nr_shared_resources);
160  if (result != DOCA_SUCCESS) {
161  DOCA_LOG_ERR("Failed to init DOCA Flow: %s", doca_error_get_descr(result));
162  return result;
163  }
164 
165  memset(dev_arr, 0, sizeof(struct doca_dev *) * nb_ports);
166  ARRAY_INIT(actions_mem_size, ACTIONS_MEM_SIZE(nb_queues, num_of_entries));
168  if (result != DOCA_SUCCESS) {
169  DOCA_LOG_ERR("Failed to init DOCA ports");
171  return result;
172  }
173 
174  for (port_id = 0; port_id < nb_ports; port_id++) {
175  memset(&status, 0, sizeof(status));
176 
177  result = create_multi_fwd_pipe(ports[port_id], &pipe);
178  if (result != DOCA_SUCCESS) {
179  DOCA_LOG_ERR("Failed to create pipe: %s", doca_error_get_descr(result));
182  return result;
183  }
184 
185  result = add_multi_fwd_pipe_entries(pipe, port_id, &status);
186  if (result != DOCA_SUCCESS) {
189  return result;
190  }
191 
192  result = doca_flow_entries_process(ports[port_id], 0, DEFAULT_TIMEOUT_US, num_of_entries);
193  if (result != DOCA_SUCCESS) {
194  DOCA_LOG_ERR("Failed to process entries: %s", doca_error_get_descr(result));
197  return result;
198  }
199 
200  if (status.nb_processed != num_of_entries || status.failure) {
201  DOCA_LOG_ERR("Failed to process entries");
204  return DOCA_ERROR_BAD_STATE;
205  }
206  }
207 
208  DOCA_LOG_INFO("Wait few seconds for packets to arrive");
209  sleep(5);
210 
213  return result;
214 }
#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)
doca_error_t flow_multi_fwd(int nb_queues)
doca_error_t create_multi_fwd_pipe(struct doca_flow_port *port, struct doca_flow_pipe **pipe)
DOCA_LOG_REGISTER(FLOW_MULTI_FWD)
doca_error_t add_multi_fwd_pipe_entries(struct doca_flow_pipe *pipe, int port_id, struct entries_status *status)
static struct doca_flow_actions actions
Definition: flow_parser.c:107
#define BE_IPV4_ADDR(a, b, c, d)
Definition: flow_parser.c:64
static struct doca_flow_fwd fwd
Definition: flow_parser.c:109
#define DEFAULT_TIMEOUT_US
Definition: flow_skeleton.c:36
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_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_EXPERIMENTAL doca_error_t doca_flow_pipe_cfg_set_actions(struct doca_flow_pipe_cfg *cfg, struct doca_flow_actions *const *actions, struct doca_flow_actions *const *actions_masks, struct doca_flow_action_descs *const *action_descs, size_t nr_actions)
Set pipe's actions, actions mask and actions descriptor.
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_FLOW_PIPE_BASIC
Definition: doca_flow.h:221
@ DOCA_FLOW_L3_META_IPV4
Definition: doca_flow.h:296
@ DOCA_FLOW_FWD_PORT
Definition: doca_flow.h:744
@ DOCA_FLOW_FWD_CHANGEABLE
Definition: doca_flow.h:756
@ DOCA_FLOW_FWD_DROP
Definition: doca_flow.h:748
#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
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 NB_ACTIONS_ARR
Definition: flow_common.h:58
#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
doca flow actions information
Definition: doca_flow.h:684
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_l3_type l3_type
Definition: doca_flow.h:446
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
enum doca_flow_l3_meta outer_l3_type
Definition: doca_flow.h:382
user context struct that will be used in entries process callback
Definition: flow_common.h:78
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