From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by dpdk.org (Postfix) with ESMTP id 86FAA1B56A for ; Wed, 11 Jul 2018 23:45:48 +0200 (CEST) Received: by mail-wr1-f65.google.com with SMTP id g6-v6so10691606wrp.0 for ; Wed, 11 Jul 2018 14:45:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=5lquyKyb4fQAQodLIsSlzwJeEaVORDxXaCUXdXbGS6g=; b=GFkctAr+aDgtIdEtp7VMjxK5cQUQX+wG7wd4QrwY+cynDpVwgR3I0E8ZhMOs9aUKdU Eme/GhmJKLhaSYeyGfId6r+joEP5MtjsN0rWC72cLk1UrNU3ZpJ3F4rPclBbJHCu6GSM W229RieEfm0hl+H62fTUW+OoJq1L/CB1xG3FjdNt668VYV1ML9vu+JA9V1Jm09MzlTjA 82pIBirBuXh0U5tcrVd69oZIjKygLLbUUL0uuaJ9xve+9i5FfuF2anaZ/mMW2H7GF/3l wXDCOx3A9W7507zXdGMk2voCF/sgmuixtwyRlfC13GP0ogD1tuUqPSgahFHycZzY+jU2 y8GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=5lquyKyb4fQAQodLIsSlzwJeEaVORDxXaCUXdXbGS6g=; b=OwutrqGH1L2K+ZUGLhyPU7nqN0UUxRR72vmevPzQL08qV3ofIm5m9g+/5lEr1NlmIJ 1uzltWZlppzK9mAt2P0JRYBP7fmVCFsddLAcQUn7Vp0rs60KE+9j2mUhhKJkr3RS7nOr AtNC+MWwMtyaRCFd4uv6TdeVBtBG29Pb0r3yGF5hVXOPaRjb1PrtXxvZSw8x7znlIakf a294jBcqoTH84v25HVrVTZd3S51IR4kjoySfqItZcwk8SQNMykIN5ei5iwIraAIkqYku GnNYfgye4KpWG1lx75zK82je6YVbebpcUlpuOs/gSBQoh31b/gpkzsXfjZfW5dZxNbNb xqnQ== X-Gm-Message-State: AOUpUlFPoBOeoq9GSErvJcxF+94WGyhfWHhiYzShB5m5uyG9Id+IRUIy XtgA2NCQZE4YvD7n7iknRtjEAlWU X-Google-Smtp-Source: AAOMgpcV8d7F0QCIxocolnoNKT4XHyX5buNUBYLy2ZdykbSvQp0IhwRr8IrptGyXz+f+Eu5lRIQMYQ== X-Received: by 2002:adf:f850:: with SMTP id d16-v6mr209105wrq.172.1531345547668; Wed, 11 Jul 2018 14:45:47 -0700 (PDT) Received: from bidouze.dev.6wind.com. (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id h5-v6sm10550936wrr.19.2018.07.11.14.45.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Jul 2018 14:45:46 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Wed, 11 Jul 2018 23:44:58 +0200 Message-Id: <31ece7bda6ce4809dd5a6941a4f47176996b7321.1531345404.git.gaetan.rivet@6wind.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: References: Subject: [dpdk-dev] [PATCH v11 08/25] devargs: add function to parse device layers X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Jul 2018 21:45:48 -0000 This function is private to the EAL. It is used to parse each layers in a device description string, and store the result in an rte_devargs structure. Signed-off-by: Gaetan Rivet --- lib/librte_eal/common/eal_common_devargs.c | 144 ++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 34 +++++ lib/librte_eal/common/include/rte_devargs.h | 13 +- 3 files changed, 188 insertions(+), 3 deletions(-) diff --git a/lib/librte_eal/common/eal_common_devargs.c b/lib/librte_eal/common/eal_common_devargs.c index f84f3d2cf..a22a2002e 100644 --- a/lib/librte_eal/common/eal_common_devargs.c +++ b/lib/librte_eal/common/eal_common_devargs.c @@ -13,9 +13,13 @@ #include #include +#include +#include #include #include #include +#include +#include #include #include #include "eal_private.h" @@ -57,6 +61,146 @@ rte_eal_parse_devargs_str(const char *devargs_str, return 0; } +static size_t +devargs_layer_count(const char *s) +{ + size_t i = s ? 1 : 0; + + while (s != NULL && s[0] != '\0') { + i += s[0] == '/'; + s++; + } + return i; +} + +int +rte_devargs_layers_parse(struct rte_devargs *devargs, + const char *devstr) +{ + struct { + const char *key; + const char *str; + struct rte_kvargs *kvlist; + } layers[] = { + { "bus=", NULL, NULL, }, + { "class=", NULL, NULL, }, + { "driver=", NULL, NULL, }, + }; + struct rte_kvargs_pair *kv = NULL; + struct rte_class *cls = NULL; + struct rte_bus *bus = NULL; + const char *s = devstr; + size_t nblayer; + size_t i = 0; + int ret = 0; + + /* Split each sub-lists. */ + nblayer = devargs_layer_count(devstr); + if (nblayer > RTE_DIM(layers)) { + RTE_LOG(ERR, EAL, "Invalid format: too many layers (%zu)\n", + nblayer); + ret = -E2BIG; + goto get_out; + } + + /* If the devargs points the devstr + * as source data, then it should not allocate + * anything and keep referring only to it. + */ + if (devargs->data != devstr) { + devargs->data = strdup(devstr); + if (devargs->data == NULL) { + RTE_LOG(ERR, EAL, "OOM\n"); + ret = -ENOMEM; + goto get_out; + } + s = devargs->data; + } + + while (s != NULL) { + if (strncmp(layers[i].key, s, + strlen(layers[i].key)) && + /* The last layer is free-form. + * The "driver" key is not required (but accepted). + */ + i != RTE_DIM(layers) - 1) + goto next_layer; + layers[i].str = s; + layers[i].kvlist = rte_kvargs_parse_delim(s, NULL, "/"); + if (layers[i].kvlist == NULL) { + RTE_LOG(ERR, EAL, "Could not parse %s\n", s); + ret = -EINVAL; + goto get_out; + } + s = strchr(s, '/'); + if (s != NULL) + s++; +next_layer: + if (i >= RTE_DIM(layers)) { + RTE_LOG(ERR, EAL, "Unrecognized layer %s\n", s); + ret = -EINVAL; + goto get_out; + } + i++; + } + + /* Parse each sub-list. */ + for (i = 0; i < RTE_DIM(layers); i++) { + if (layers[i].kvlist == NULL) + continue; + kv = &layers[i].kvlist->pairs[0]; + if (strcmp(kv->key, "bus") == 0) { + bus = rte_bus_find_by_name(kv->value); + if (bus == NULL) { + RTE_LOG(ERR, EAL, "Could not find bus \"%s\"\n", + kv->value); + ret = -EFAULT; + goto get_out; + } + } else if (strcmp(kv->key, "class") == 0) { + cls = rte_class_find_by_name(kv->value); + if (cls == NULL) { + RTE_LOG(ERR, EAL, "Could not find class \"%s\"\n", + kv->value); + ret = -EFAULT; + goto get_out; + } + } else if (strcmp(kv->key, "driver") == 0) { + /* Ignore */ + continue; + } + } + + /* Fill devargs fields. */ + devargs->bus_str = layers[0].str; + devargs->cls_str = layers[1].str; + devargs->drv_str = layers[2].str; + devargs->bus = bus; + devargs->cls = cls; + + /* If we own the data, clean up a bit + * the several layers string, to ease + * their parsing afterward. + */ + if (devargs->data != devstr) { + char *s = (void *)(intptr_t)(devargs->data); + + while ((s = strchr(s, '/'))) { + *s = '\0'; + s++; + } + } + +get_out: + for (i = 0; i < RTE_DIM(layers); i++) { + if (layers[i].kvlist) + rte_kvargs_free(layers[i].kvlist); + } + if (ret != 0) + rte_errno = -ret; + return ret; +} + static int bus_name_cmp(const struct rte_bus *bus, const void *name) { diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index bdadc4d50..722957428 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -258,4 +258,38 @@ int rte_mp_channel_init(void); */ void dev_callback_process(char *device_name, enum rte_dev_event_type event); +/** + * @internal + * Parse a device string and store its information in an + * rte_devargs structure. + * + * A device description is split by layers of abstraction of the device: + * bus, class and driver. Each layer will offer a set of properties that + * can be applied either to configure or recognize a device. + * + * This function will parse those properties and prepare the rte_devargs + * to be given to each layers for processing. + * + * Note: if the "data" field of the devargs points to devstr, + * then no dynamic allocation is performed and the rte_devargs + * can be safely discarded. + * + * Otherwise ``data`` will hold a workable copy of devstr, that will be + * used by layers descriptors within rte_devargs. In this case, + * any rte_devargs should be cleaned-up before being freed. + * + * @param da + * rte_devargs structure to fill. + * + * @param devstr + * Device string. + * + * @return + * 0 on success. + * Negative errno values on error (rte_errno is set). + */ +int +rte_devargs_layers_parse(struct rte_devargs *devargs, + const char *devstr); + #endif /* _EAL_PRIVATE_H_ */ diff --git a/lib/librte_eal/common/include/rte_devargs.h b/lib/librte_eal/common/include/rte_devargs.h index 6c3b6326b..097a4ce7b 100644 --- a/lib/librte_eal/common/include/rte_devargs.h +++ b/lib/librte_eal/common/include/rte_devargs.h @@ -51,12 +51,19 @@ struct rte_devargs { enum rte_devtype type; /** Device policy. */ enum rte_dev_policy policy; - /** Bus handle for the device. */ - struct rte_bus *bus; /** Name of the device. */ char name[RTE_DEV_NAME_MAX_LEN]; + RTE_STD_C11 + union { /** Arguments string as given by user or "" for no argument. */ - char *args; + char *args; + const char *drv_str; + }; + struct rte_bus *bus; /**< bus handle. */ + struct rte_class *cls; /**< class handle. */ + const char *bus_str; /**< bus-related part of device string. */ + const char *cls_str; /**< class-related part of device string. */ + const char *data; /**< Device string storage. */ }; /** -- 2.18.0