Skip to content

Commit

Permalink
simap: New data structure for string-to-integer maps.
Browse files Browse the repository at this point in the history
This commit adapts a couple of existing pieces of code to use the
new data structure.  The following commit will add another user
(which is also the first use of the simap_increas() function).

Signed-off-by: Ben Pfaff <[email protected]>
  • Loading branch information
blp committed May 22, 2012
1 parent b54c9e9 commit 44bac24
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 36 deletions.
2 changes: 2 additions & 0 deletions lib/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ lib_libopenvswitch_a_SOURCES = \
lib/sha1.h \
lib/shash.c \
lib/shash.h \
lib/simap.c \
lib/simap.h \
lib/signals.c \
lib/signals.h \
lib/socket-util.c \
Expand Down
7 changes: 4 additions & 3 deletions lib/learning-switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "poll-loop.h"
#include "rconn.h"
#include "shash.h"
#include "simap.h"
#include "timeval.h"
#include "vconn.h"
#include "vlog.h"
Expand Down Expand Up @@ -125,12 +126,12 @@ lswitch_create(struct rconn *rconn, const struct lswitch_config *cfg)
hmap_init(&sw->queue_numbers);
shash_init(&sw->queue_names);
if (cfg->port_queues) {
struct shash_node *node;
struct simap_node *node;

SHASH_FOR_EACH (node, cfg->port_queues) {
SIMAP_FOR_EACH (node, cfg->port_queues) {
struct lswitch_port *port = xmalloc(sizeof *port);
hmap_node_nullify(&port->hmap_node);
port->queue_id = (uintptr_t) node->data;
port->queue_id = node->data;
shash_add(&sw->queue_names, node->name, port);
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/learning-switch.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2010, 2011 Nicira, Inc.
* Copyright (c) 2008, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -55,8 +55,8 @@ struct lswitch_config {
* specifying a particular queue. */
uint32_t default_queue;

/* Maps from a port name to a queue_id (cast to void *). */
const struct shash *port_queues;
/* Maps from a port name to a queue_id. */
const struct simap *port_queues;
};

struct lswitch *lswitch_create(struct rconn *, const struct lswitch_config *);
Expand Down
31 changes: 15 additions & 16 deletions lib/odp-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "ofpbuf.h"
#include "openvswitch/tunnel.h"
#include "packets.h"
#include "shash.h"
#include "simap.h"
#include "timeval.h"
#include "util.h"
#include "vlog.h"
Expand All @@ -49,7 +49,7 @@ VLOG_DEFINE_THIS_MODULE(odp_util);
* from another. */
static const char *delimiters = ", \t\r\n";

static int parse_odp_key_attr(const char *, const struct shash *port_names,
static int parse_odp_key_attr(const char *, const struct simap *port_names,
struct ofpbuf *);
static void format_odp_key_attr(const struct nlattr *a, struct ds *ds);

Expand Down Expand Up @@ -355,7 +355,7 @@ format_odp_actions(struct ds *ds, const struct nlattr *actions,
}

static int
parse_odp_action(const char *s, const struct shash *port_names,
parse_odp_action(const char *s, const struct simap *port_names,
struct ofpbuf *actions)
{
/* Many of the sscanf calls in this function use oversized destination
Expand All @@ -380,12 +380,11 @@ parse_odp_action(const char *s, const struct shash *port_names,

if (port_names) {
int len = strcspn(s, delimiters);
struct shash_node *node;
struct simap_node *node;

node = shash_find_len(port_names, s, len);
node = simap_find_len(port_names, s, len);
if (node) {
nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT,
(uintptr_t) node->data);
nl_msg_put_u32(actions, OVS_ACTION_ATTR_OUTPUT, node->data);
return len;
}
}
Expand Down Expand Up @@ -561,7 +560,7 @@ parse_odp_action(const char *s, const struct shash *port_names,
* Netlink attributes. On failure, no data is appended to 'actions'. Either
* way, 'actions''s data might be reallocated. */
int
odp_actions_from_string(const char *s, const struct shash *port_names,
odp_actions_from_string(const char *s, const struct simap *port_names,
struct ofpbuf *actions)
{
size_t old_size;
Expand Down Expand Up @@ -878,7 +877,7 @@ ovs_frag_type_from_string(const char *s, enum ovs_frag_type *type)
}

static int
parse_odp_key_attr(const char *s, const struct shash *port_names,
parse_odp_key_attr(const char *s, const struct simap *port_names,
struct ofpbuf *key)
{
/* Many of the sscanf calls in this function use oversized destination
Expand Down Expand Up @@ -925,14 +924,14 @@ parse_odp_key_attr(const char *s, const struct shash *port_names,

if (port_names && !strncmp(s, "in_port(", 8)) {
const char *name;
const struct shash_node *node;
const struct simap_node *node;
int name_len;

name = s + 8;
name_len = strcspn(s, ")");
node = shash_find_len(port_names, name, name_len);
node = simap_find_len(port_names, name, name_len);
if (node) {
nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, (uintptr_t) node->data);
nl_msg_put_u32(key, OVS_KEY_ATTR_IN_PORT, node->data);
return 8 + name_len + 1;
}
}
Expand Down Expand Up @@ -1209,15 +1208,15 @@ parse_odp_key_attr(const char *s, const struct shash *port_names,
* data is appended to 'key'. Either way, 'key''s data might be
* reallocated.
*
* If 'port_names' is nonnull, it points to an shash that maps from a port name
* to a port number cast to void *. (Port names may be used instead of port
* numbers in in_port.)
* If 'port_names' is nonnull, it points to an simap that maps from a port name
* to a port number. (Port names may be used instead of port numbers in
* in_port.)
*
* On success, the attributes appended to 'key' are individually syntactically
* valid, but they may not be valid as a sequence. 'key' might, for example,
* have duplicated keys. odp_flow_key_to_flow() will detect those errors. */
int
odp_flow_key_from_string(const char *s, const struct shash *port_names,
odp_flow_key_from_string(const char *s, const struct simap *port_names,
struct ofpbuf *key)
{
const size_t old_size = key->size;
Expand Down
6 changes: 3 additions & 3 deletions lib/odp-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ds;
struct flow;
struct nlattr;
struct ofpbuf;
struct shash;
struct simap;

#define OVSP_NONE ((uint16_t) -1)

Expand Down Expand Up @@ -62,7 +62,7 @@ odp_port_to_ofp_port(uint16_t odp_port)

void format_odp_actions(struct ds *, const struct nlattr *odp_actions,
size_t actions_len);
int odp_actions_from_string(const char *, const struct shash *port_names,
int odp_actions_from_string(const char *, const struct simap *port_names,
struct ofpbuf *odp_actions);

/* Upper bound on the length of a nlattr-formatted flow key. The longest
Expand Down Expand Up @@ -92,7 +92,7 @@ struct odputil_keybuf {
};

void odp_flow_key_format(const struct nlattr *, size_t, struct ds *);
int odp_flow_key_from_string(const char *s, const struct shash *port_names,
int odp_flow_key_from_string(const char *s, const struct simap *port_names,
struct ofpbuf *);

void odp_flow_key_from_flow(struct ofpbuf *, const struct flow *);
Expand Down
Loading

0 comments on commit 44bac24

Please sign in to comment.