1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright 2021 6WIND S.A.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rte_common.h>
#include <rte_kvargs.h>
#include <bus_driver.h>
#include <rte_bus_vdev.h>
#include "test.h"
#define TEST_VDEV_KEY_NAME "name"
static const char * const valid_keys[] = {
TEST_VDEV_KEY_NAME,
NULL,
};
static int
cmp_dev_name(const struct rte_device *dev, const void *name)
{
return strcmp(rte_dev_name(dev), name);
}
static int
cmp_dev_match(const struct rte_device *dev, const void *_kvlist)
{
const struct rte_kvargs *kvlist = _kvlist;
const char *key = TEST_VDEV_KEY_NAME;
const char *name;
/* no kvlist arg, all devices match */
if (kvlist == NULL)
return 0;
/* if key is present in kvlist and does not match, filter device */
name = rte_kvargs_get(kvlist, key);
if (name != NULL && strcmp(name, rte_dev_name(dev)) != 0)
return -1;
return 0;
}
static struct rte_device *
get_matching_vdev(const char *match_str)
{
struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
struct rte_kvargs *kvargs = NULL;
struct rte_device *dev;
if (match_str != NULL) {
kvargs = rte_kvargs_parse(match_str, valid_keys);
if (kvargs == NULL) {
printf("Failed to parse match string\n");
return NULL;
}
}
dev = vdev_bus->find_device(NULL, cmp_dev_match, kvargs);
rte_kvargs_free(kvargs);
return dev;
}
static int
test_vdev_bus(void)
{
struct rte_bus *vdev_bus = rte_bus_find_by_name("vdev");
struct rte_dev_iterator dev_iter = { 0 };
struct rte_device *dev, *dev0, *dev1;
/* not supported */
if (vdev_bus == NULL)
return 0;
/* create first vdev */
if (rte_vdev_init("net_null_test0", "") < 0) {
printf("Failed to create vdev net_null_test0\n");
goto fail;
}
dev0 = vdev_bus->find_device(NULL, cmp_dev_name, "net_null_test0");
if (dev0 == NULL) {
printf("Cannot find net_null_test0 vdev\n");
goto fail;
}
/* create second vdev */
if (rte_vdev_init("net_null_test1", "") < 0) {
printf("Failed to create vdev net_null_test1\n");
goto fail;
}
dev1 = vdev_bus->find_device(NULL, cmp_dev_name, "net_null_test1");
if (dev1 == NULL) {
printf("Cannot find net_null_test1 vdev\n");
goto fail;
}
/* try to match vdevs */
dev = get_matching_vdev("name=net_null_test0");
if (dev != dev0) {
printf("Cannot match net_null_test0 vdev\n");
goto fail;
}
dev = get_matching_vdev("name=net_null_test1");
if (dev != dev1) {
printf("Cannot match net_null_test1 vdev\n");
goto fail;
}
dev = get_matching_vdev("name=unexistant");
if (dev != NULL) {
printf("Unexistant vdev should not match\n");
goto fail;
}
dev = get_matching_vdev("");
if (dev == NULL || dev == dev1) {
printf("Cannot match any vdev with empty match string\n");
goto fail;
}
dev = get_matching_vdev(NULL);
if (dev == NULL || dev == dev1) {
printf("Cannot match any vdev with NULL match string\n");
goto fail;
}
/* iterate all vdevs, and ensure we find vdev0 and vdev1 */
RTE_DEV_FOREACH(dev, "bus=vdev", &dev_iter) {
if (dev == dev0)
dev0 = NULL;
else if (dev == dev1)
dev1 = NULL;
}
if (dev0 != NULL) {
printf("dev0 was not iterated\n");
goto fail;
}
if (dev1 != NULL) {
printf("dev1 was not iterated\n");
goto fail;
}
rte_vdev_uninit("net_null_test0");
rte_vdev_uninit("net_null_test1");
return 0;
fail:
rte_vdev_uninit("net_null_test0");
rte_vdev_uninit("net_null_test1");
return -1;
}
static int
test_vdev(void)
{
printf("== test vdev bus ==\n");
if (test_vdev_bus() < 0)
return -1;
return 0;
}
REGISTER_FAST_TEST(vdev_autotest, true, true, test_vdev);
|