/* * Copyright (c) 2023-2026 NVIDIA CORPORATION AND AFFILIATES. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are permitted * provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used * to endorse or promote products derived from this software without specific prior written * permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include #include #include #include DOCA_LOG_REGISTER(FLOW_SWITCH); #define NB_ENTRIES 2 static struct doca_flow_pipe_entry *entries[2 * NB_ENTRIES]; /* array for storing created entries */ /* * Create DOCA Flow pipe with 5 tuple match on the switch port. * Matched traffic will be forwarded to the port defined per entry. * Unmatched traffic will be dropped. * * @sw_port [in]: switch port * @pipe [out]: created pipe pointer * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise. */ static doca_error_t create_switch_pipe(struct doca_flow_port *sw_port, struct doca_flow_pipe **pipe) { struct doca_flow_match match; struct doca_flow_monitor monitor; struct doca_flow_fwd fwd; struct doca_flow_fwd fwd_miss; struct doca_flow_pipe_cfg *pipe_cfg; doca_error_t result; memset(&match, 0, sizeof(match)); memset(&monitor, 0, sizeof(monitor)); memset(&fwd, 0, sizeof(fwd)); memset(&fwd_miss, 0, sizeof(fwd_miss)); match.parser_meta.outer_l3_type = DOCA_FLOW_L3_META_IPV4; match.parser_meta.outer_l4_type = DOCA_FLOW_L4_META_TCP; match.outer.l4_type_ext = DOCA_FLOW_L4_TYPE_EXT_TCP; match.outer.l3_type = DOCA_FLOW_L3_TYPE_IP4; /* Source, destination IP addresses and source, destination TCP ports are defined per entry */ match.outer.ip4.src_ip = 0xffffffff; match.outer.ip4.dst_ip = 0xffffffff; match.outer.tcp.l4_port.src_port = 0xffff; match.outer.tcp.l4_port.dst_port = 0xffff; fwd.type = DOCA_FLOW_FWD_PORT; /* Port ID to forward to is defined per entry */ fwd.port_id = 0xffff; /* Unmatched packets will be dropped */ fwd_miss.type = DOCA_FLOW_FWD_DROP; monitor.counter_type = DOCA_FLOW_RESOURCE_TYPE_NON_SHARED; result = doca_flow_pipe_cfg_create(&pipe_cfg, sw_port); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to create doca_flow_pipe_cfg: %s", doca_error_get_descr(result)); return result; } result = set_flow_pipe_cfg(pipe_cfg, "SWITCH_PIPE", DOCA_FLOW_PIPE_BASIC, true); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg: %s", doca_error_get_descr(result)); goto destroy_pipe_cfg; } result = doca_flow_pipe_cfg_set_nr_entries(pipe_cfg, NB_ENTRIES); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg nr_entries: %s", doca_error_get_descr(result)); goto destroy_pipe_cfg; } result = doca_flow_pipe_cfg_set_match(pipe_cfg, &match, NULL); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg match: %s", doca_error_get_descr(result)); goto destroy_pipe_cfg; } result = doca_flow_pipe_cfg_set_monitor(pipe_cfg, &monitor); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to set doca_flow_pipe_cfg monitor: %s", doca_error_get_descr(result)); goto destroy_pipe_cfg; } result = doca_flow_pipe_create(pipe_cfg, &fwd, &fwd_miss, pipe); destroy_pipe_cfg: doca_flow_pipe_cfg_destroy(pipe_cfg); return result; } /* * Add DOCA Flow pipe entry to the pipe * * @switch_num [in]: switch number (1, 2 ...) * @pipe [in]: pipe of the entry * @status [in]: user context for adding entry * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise. */ static doca_error_t add_switch_pipe_entries(int switch_num, struct doca_flow_pipe *pipe, struct entries_status *status) { struct doca_flow_match match; struct doca_flow_fwd fwd; uint32_t flags = DOCA_FLOW_ENTRY_FLAGS_WAIT_FOR_BATCH; doca_error_t result; int entry_index = 0; int port_base; int entry_base = (switch_num - 1) * 2; port_base = (switch_num - 1) * 3; doca_be32_t dst_ip_addr; doca_be32_t src_ip_addr; doca_be16_t dst_port; doca_be16_t src_port; memset(&fwd, 0, sizeof(fwd)); memset(&match, 0, sizeof(match)); for (entry_index = 0; entry_index < NB_ENTRIES; entry_index++) { dst_ip_addr = BE_IPV4_ADDR(8, 8, 8, 8 + entry_base + entry_index); src_ip_addr = BE_IPV4_ADDR(1, 2, 3, 4 + entry_base + entry_index); dst_port = DOCA_HTOBE16(80); src_port = DOCA_HTOBE16(1234); match.outer.ip4.dst_ip = dst_ip_addr; match.outer.ip4.src_ip = src_ip_addr; match.outer.tcp.l4_port.dst_port = dst_port; match.outer.tcp.l4_port.src_port = src_port; fwd.type = DOCA_FLOW_FWD_PORT; fwd.port_id = port_base + 1 + entry_index; /* The port to forward to is defined based on the entry index */ /* last entry should be inserted with DOCA_FLOW_ENTRY_FLAGS_NO_WAIT flag */ if (entry_index == NB_ENTRIES - 1) flags = DOCA_FLOW_ENTRY_FLAGS_NO_WAIT; result = doca_flow_pipe_basic_add_entry(0, pipe, &match, 0, NULL, NULL, &fwd, flags, status, &entries[entry_base + entry_index]); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to add pipe entry: %s", doca_error_get_descr(result)); return result; } } return DOCA_SUCCESS; } /* * Run flow_switch sample * * @nb_queues [in]: number of queues the sample will use * @nb_ports [in]: number of ports the sample will use * @devs_manager [in]: Array of DOCA devices for the switch ports * @nb_devs [in]: Amount of eswitch manager dev bundles in the switch_manager_devs array * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise. */ /* Context structure for statistics printing */ struct switch_stats_context { struct doca_flow_pipe_entry **entries; }; /* * Print switch statistics * * @entries [in]: array of flow entries */ static void print_switch_stats(struct doca_flow_pipe_entry *entries[]) { doca_error_t result; struct doca_flow_resource_query query_stats; int entry_idx; /* dump entries counters */ for (entry_idx = 0; entry_idx < 2 * NB_ENTRIES; entry_idx++) { result = doca_flow_resource_query_entry(entries[entry_idx], &query_stats); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to query entry: %s", doca_error_get_descr(result)); return; } DOCA_LOG_INFO("Entry in index: %d", entry_idx); DOCA_LOG_INFO("Total bytes: %ld", query_stats.counter.total_bytes); DOCA_LOG_INFO("Total packets: %ld", query_stats.counter.total_pkts); } } /* * Wrapper function for statistics printing compatible with flow_wait_for_packets * * @context [in]: switch_stats_context structure */ static void print_switch_stats_wrapper(void *context) { struct switch_stats_context *ctx = (struct switch_stats_context *)context; print_switch_stats(ctx->entries); } doca_error_t flow_switch(int nb_queues, int nb_ports, struct flow_devs_manager devs_manager[], uint16_t nb_devs) { struct flow_resources resource = {0}; uint32_t nr_shared_resources[SHARED_RESOURCE_NUM_VALUES] = {0}; struct doca_flow_port *ports[nb_ports]; uint32_t actions_mem_size[nb_ports]; struct doca_flow_pipe *pipe1; struct doca_flow_pipe *pipe2; struct entries_status status; doca_error_t result; memset(&status, 0, sizeof(status)); resource.mode = DOCA_FLOW_RESOURCE_MODE_PORT; resource.nr_counters = 2 * NB_ENTRIES; /* counter per entry */ result = init_doca_flow(nb_queues, "switch,hws", &resource, nr_shared_resources); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to init DOCA Flow: %s", doca_error_get_descr(result)); return result; } ARRAY_INIT(actions_mem_size, ACTIONS_MEM_SIZE(NB_ENTRIES)); result = init_doca_flow_switch_ports(devs_manager, nb_devs, ports, nb_ports, actions_mem_size, &resource); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to init DOCA ports: %s", doca_error_get_descr(result)); doca_flow_destroy(); return result; } result = create_switch_pipe(doca_flow_port_switch_get(ports[0]), &pipe1); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to create pipe: %s", doca_error_get_descr(result)); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; } result = add_switch_pipe_entries(1, pipe1, &status); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to add entries to the pipe: %s", doca_error_get_descr(result)); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; } result = doca_flow_pipe_queue_drain(doca_flow_port_switch_get(ports[0]), 0, NULL); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to process entries: %s", doca_error_get_descr(result)); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; } result = create_switch_pipe(doca_flow_port_switch_get(ports[3]), &pipe2); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to create pipe: %s", doca_error_get_descr(result)); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; } result = add_switch_pipe_entries(2, pipe2, &status); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to add entries to the pipe: %s", doca_error_get_descr(result)); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; } result = doca_flow_pipe_queue_drain(doca_flow_port_switch_get(ports[3]), 0, NULL); if (result != DOCA_SUCCESS) { DOCA_LOG_ERR("Failed to process entries: %s", doca_error_get_descr(result)); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; } if (status.nb_processed != 2 * NB_ENTRIES || status.failure) { DOCA_LOG_ERR("Failed to process entries"); stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return DOCA_ERROR_BAD_STATE; } /* Setup statistics context and wait for packets */ struct switch_stats_context stats_ctx = {.entries = entries}; flow_wait_for_packets(15, print_switch_stats_wrapper, &stats_ctx); result = stop_doca_flow_ports(nb_ports, ports); doca_flow_destroy(); return result; }