NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
flow_entropy_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 <rte_byteorder.h>
30 
31 #include <doca_log.h>
32 #include <doca_flow.h>
33 
34 #include "flow_common.h"
35 #include "flow_switch_common.h"
36 
37 DOCA_LOG_REGISTER(FLOW_ENTROPY);
38 
39 /*
40  * Entropy sample does not create flows.
41  * Allocate minimal resources.
42  */
43 #define DOCA_SAMPLE_ENTROPY_QUEUES_NUM 1
44 #define DOCA_SAMPLE_ENTROPY_ENTRIES_NUM 1
45 
46 /*
47  * Run flow_entropy sample
48  *
49  * @ctx [in]: flow switch context the sample will use
50  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise.
51  */
53 {
54  const int nb_ports = 1;
55  uint32_t actions_mem_size[nb_ports];
56  struct flow_resources resource = {0};
57  uint32_t nr_shared_resources[SHARED_RESOURCE_NUM_VALUES] = {0};
58  struct doca_flow_port *ports[nb_ports];
59  struct doca_dev *dev_arr[nb_ports];
60  struct doca_flow_entropy_format header;
61  uint16_t entropy;
63 
64  result = init_doca_flow(1, "switch,hws", &resource, nr_shared_resources);
65  if (result != DOCA_SUCCESS) {
66  DOCA_LOG_ERR("Failed to init DOCA Flow: %s", doca_error_get_descr(result));
67  return result;
68  }
69 
70  memset(dev_arr, 0, sizeof(struct doca_dev *) * nb_ports);
71  dev_arr[0] = ctx->doca_dev[0];
74  if (result != DOCA_SUCCESS) {
75  DOCA_LOG_ERR("Failed to init DOCA ports: %s", doca_error_get_descr(result));
77  return result;
78  }
79 
81  header.ip4.src_ip = BE_IPV4_ADDR(8, 8, 8, 8);
82  header.ip4.dst_ip = BE_IPV4_ADDR(7, 7, 7, 7);
84  header.transport.src_port = rte_cpu_to_be_16(1234);
85  header.transport.dst_port = rte_cpu_to_be_16(5678);
86 
87  /* The entropy result should be equal to 0xdb9e */
88  result = doca_flow_port_calc_entropy(ports[0], &header, &entropy);
89  if (result == DOCA_SUCCESS)
90  DOCA_LOG_INFO("The entropy for the given packet header is:0x%x", rte_be_to_cpu_16(entropy));
91  else
92  DOCA_LOG_ERR("Failed to retrieve entropy: %s", doca_error_get_descr(result));
93 
96  return result;
97 }
int32_t result
struct doca_flow_port * init_doca_flow(uint16_t port_id, uint8_t rxq_num)
Definition: flow.c:37
#define DOCA_SAMPLE_ENTROPY_QUEUES_NUM
DOCA_LOG_REGISTER(FLOW_ENTROPY)
doca_error_t flow_entropy(struct flow_switch_ctx *ctx)
#define DOCA_SAMPLE_ENTROPY_ENTRIES_NUM
#define BE_IPV4_ADDR(a, b, c, d)
Definition: flow_parser.c:64
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_SUCCESS
Definition: doca_error.h:38
@ DOCA_FLOW_L4_TYPE_EXT_UDP
@ DOCA_FLOW_L3_TYPE_IP4
DOCA_EXPERIMENTAL doca_error_t doca_flow_port_calc_entropy(struct doca_flow_port *port, struct doca_flow_entropy_format *header, uint16_t *entropy)
Calculate the entropy.
DOCA_STABLE void doca_flow_destroy(void)
Destroy the doca flow.
#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
#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 header format for entropy
Definition: doca_flow.h:473
struct doca_flow_header_l4_port transport
Definition: doca_flow.h:484
enum doca_flow_l4_type_ext l4_type_ext
Definition: doca_flow.h:482
enum doca_flow_l3_type l3_type
Definition: doca_flow.h:474
struct doca_flow_header_ip4 ip4
Definition: doca_flow.h:477
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