From 4afba28d559511a5ee03e005b65be8323bf26ad3 Mon Sep 17 00:00:00 2001 From: Justin Pettit Date: Wed, 17 Oct 2012 23:11:53 -0700 Subject: [PATCH] dpif: Add new dpif_port_exists() function. Provide the ability to determine whether a port exists in a datapath without having to deal with a "dpif_port" structure as with dpif_port_query_by_name(). A future patch will use this function. Signed-off-by: Justin Pettit --- lib/dpif-linux.c | 2 +- lib/dpif-netdev.c | 4 ++-- lib/dpif-provider.h | 10 ++++++---- lib/dpif.c | 14 ++++++++++++++ lib/dpif.h | 1 + 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/dpif-linux.c b/lib/dpif-linux.c index 6b36ef60..75bfc458 100644 --- a/lib/dpif-linux.c +++ b/lib/dpif-linux.c @@ -492,7 +492,7 @@ dpif_linux_port_query__(const struct dpif *dpif, uint32_t port_no, /* A query by name reported that 'port_name' is in some datapath * other than 'dpif', but the caller wants to know about 'dpif'. */ error = ENODEV; - } else { + } else if (dpif_port) { dpif_port->name = xstrdup(reply.name); dpif_port->type = xstrdup(netdev_vport_get_netdev_type(&reply)); dpif_port->port_no = reply.port_no; diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index 7f91f48b..a80b1b06 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -522,7 +522,7 @@ dpif_netdev_port_query_by_number(const struct dpif *dpif, uint32_t port_no, int error; error = get_port_by_number(dp, port_no, &port); - if (!error) { + if (!error && dpif_port) { answer_port_query(port, dpif_port); } return error; @@ -537,7 +537,7 @@ dpif_netdev_port_query_by_name(const struct dpif *dpif, const char *devname, int error; error = get_port_by_name(dp, devname, &port); - if (!error) { + if (!error && dpif_port) { answer_port_query(port, dpif_port); } return error; diff --git a/lib/dpif-provider.h b/lib/dpif-provider.h index c51b1dfa..bc189429 100644 --- a/lib/dpif-provider.h +++ b/lib/dpif-provider.h @@ -122,11 +122,13 @@ struct dpif_class { /* Removes port numbered 'port_no' from 'dpif'. */ int (*port_del)(struct dpif *dpif, uint32_t port_no); - /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. Stores - * information about the port into '*port' if successful. + /* Queries 'dpif' for a port with the given 'port_no' or 'devname'. + * If 'port' is not null, stores information about the port into + * '*port' if successful. * - * The caller takes ownership of data in 'port' and must free it with - * dpif_port_destroy() when it is no longer needed. */ + * If 'port' is not null, the caller takes ownership of data in + * 'port' and must free it with dpif_port_destroy() when it is no + * longer needed. */ int (*port_query_by_number)(const struct dpif *dpif, uint32_t port_no, struct dpif_port *port); int (*port_query_by_name)(const struct dpif *dpif, const char *devname, diff --git a/lib/dpif.c b/lib/dpif.c index d8ca0617..952a502d 100644 --- a/lib/dpif.c +++ b/lib/dpif.c @@ -486,6 +486,20 @@ dpif_port_destroy(struct dpif_port *dpif_port) free(dpif_port->type); } +/* Checks if port named 'devname' exists in 'dpif'. If so, returns + * true; otherwise, returns false. */ +bool +dpif_port_exists(const struct dpif *dpif, const char *devname) +{ + int error = dpif->dpif_class->port_query_by_name(dpif, devname, NULL); + if (error != 0 && error != ENODEV) { + VLOG_WARN_RL(&error_rl, "%s: failed to query port %s: %s", + dpif_name(dpif), devname, strerror(error)); + } + + return !error; +} + /* Looks up port number 'port_no' in 'dpif'. On success, returns 0 and * initializes '*port' appropriately; on failure, returns a positive errno * value. diff --git a/lib/dpif.h b/lib/dpif.h index db08d785..bd6095ac 100644 --- a/lib/dpif.h +++ b/lib/dpif.h @@ -85,6 +85,7 @@ struct dpif_port { }; void dpif_port_clone(struct dpif_port *, const struct dpif_port *); void dpif_port_destroy(struct dpif_port *); +bool dpif_port_exists(const struct dpif *dpif, const char *devname); int dpif_port_query_by_number(const struct dpif *, uint32_t port_no, struct dpif_port *); int dpif_port_query_by_name(const struct dpif *, const char *devname, -- 2.30.2