Skip to content

Commit

Permalink
ovn-nbctl: Add basic port group commands.
Browse files Browse the repository at this point in the history
This adds the following commands:

pg-add: Add a new port group, optionally adding switch ports at
creation.
pg-set-ports: Sets the logical switch ports on a port group
pg-del: Remove a port group.

The main motivation for these commands is that it allows for adding
logical switch ports by name rather than UUID.

Signed-off-by: Mark Michelson <mmichels@redhat.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
  • Loading branch information
putnopvut authored and blp committed Oct 11, 2018
1 parent 87e816e commit 39cc92a
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 1 deletion.
22 changes: 22 additions & 0 deletions ovn/utilities/ovn-nbctl.8.xml
Expand Up @@ -881,6 +881,28 @@
</dd>
</dl>

<h1>Port Group commands</h1>

<dl>
<dt><code>pg-add</code> <var>group</var> [<var>port</var>]...</dt>
<dd>
Creates a new port group in the <code>Port_Group</code> table named
<code>group</code> with optional <code>ports</code> added to the group.
</dd>

<dt><code>pg-set-ports</code> <var>group</var> <var>port</var>...</dt>
<dd>
Sets <code>ports</code> on the port group named <code>group</code>. It
is an error if <code>group</code> does not exist.
</dd>

<dt><code>pg-del</code> <var>group</var></dt>
<dd>
Deletes port group <code>group</code>. It is an error if
<code>group</code> does not exist.
</dd>
</dl>

<h1>Database Commands</h1>
<p>These commands query and modify the contents of <code>ovsdb</code> tables.
They are a slight abstraction of the <code>ovsdb</code> interface and
Expand Down
76 changes: 76 additions & 0 deletions ovn/utilities/ovn-nbctl.c
Expand Up @@ -686,6 +686,10 @@ SSL commands:\n\
del-ssl delete the SSL configuration\n\
set-ssl PRIV-KEY CERT CA-CERT [SSL-PROTOS [SSL-CIPHERS]] \
set the SSL configuration\n\
Port group commands:\n\
pg-add PG [PORTS] Create port group PG with optional PORTS\n\
pg-set-ports PG PORTS Set PORTS on port group PG\n\
pg-del PG Delete port group PG\n\
\n\
%s\
%s\
Expand Down Expand Up @@ -4655,6 +4659,73 @@ cmd_set_ssl(struct ctl_context *ctx)
nbrec_nb_global_set_ssl(nb_global, ssl);
}

static char *
set_ports_on_pg(struct ctl_context *ctx, const struct nbrec_port_group *pg,
char **new_ports, size_t num_new_ports)
{
struct nbrec_logical_switch_port **lports;
lports = xmalloc(sizeof *lports * num_new_ports);

size_t i;
char *error = NULL;
for (i = 0; i < num_new_ports; i++) {
const struct nbrec_logical_switch_port *lsp;
error = lsp_by_name_or_uuid(ctx, new_ports[i], true, &lsp);
if (error) {
goto out;
}
lports[i] = (struct nbrec_logical_switch_port *) lsp;
}

nbrec_port_group_set_ports(pg, lports, num_new_ports);

out:
free(lports);
return error;
}

static void
cmd_pg_add(struct ctl_context *ctx)
{
const struct nbrec_port_group *pg;

pg = nbrec_port_group_insert(ctx->txn);
nbrec_port_group_set_name(pg, ctx->argv[1]);
if (ctx->argc > 2) {
ctx->error = set_ports_on_pg(ctx, pg, ctx->argv + 2, ctx->argc - 2);
}
}

static void
cmd_pg_set_ports(struct ctl_context *ctx)
{
const struct nbrec_port_group *pg;

char *error;
error = pg_by_name_or_uuid(ctx, ctx->argv[1], true, &pg);
if (error) {
ctx->error = error;
return;
}

ctx->error = set_ports_on_pg(ctx, pg, ctx->argv + 2, ctx->argc - 2);
}

static void
cmd_pg_del(struct ctl_context *ctx)
{
const struct nbrec_port_group *pg;

char *error;
error = pg_by_name_or_uuid(ctx, ctx->argv[1], true, &pg);
if (error) {
ctx->error = error;
return;
}

nbrec_port_group_delete(pg);
}

static const struct ctl_table_class tables[NBREC_N_TABLES] = {
[NBREC_TABLE_DHCP_OPTIONS].row_ids
= {{&nbrec_logical_switch_port_col_name, NULL,
Expand Down Expand Up @@ -5123,6 +5194,11 @@ static const struct ctl_command_syntax nbctl_commands[] = {
"PRIVATE-KEY CERTIFICATE CA-CERT [SSL-PROTOS [SSL-CIPHERS]]",
pre_cmd_set_ssl, cmd_set_ssl, NULL, "--bootstrap", RW},

/* Port Group Commands */
{"pg-add", 1, INT_MAX, "", NULL, cmd_pg_add, NULL, "", RW },
{"pg-set-ports", 2, INT_MAX, "", NULL, cmd_pg_set_ports, NULL, "", RW },
{"pg-del", 1, 1, "", NULL, cmd_pg_del, NULL, "", RW },

{NULL, 0, 0, NULL, NULL, NULL, NULL, "", RO},
};

Expand Down
34 changes: 33 additions & 1 deletion tests/ovn-nbctl.at
Expand Up @@ -1518,7 +1518,6 @@ AT_CHECK([ovn-nbctl get Port_Group pg0 name], [0], [dnl
"pg0"
])])


OVN_NBCTL_TEST([ovn_nbctl_extra_newlines], [extra newlines], [
dnl This test addresses a specific issue seen when running ovn-nbctl in
dnl daemon mode. All we have to do is ensure that each time we list database
Expand All @@ -1539,3 +1538,36 @@ AT_CHECK([ovn-nbctl ls-add sw1], [0], [ignore])
AT_CHECK([ovn-nbctl --bare --columns=name list logical_switch sw1], [0], [dnl
sw1
])])
dnl ---------------------------------------------------------------------

OVN_NBCTL_TEST([ovn_nbctl_port_group_commands], [port group commands], [
AT_CHECK([ovn-nbctl pg-add pg1], [0], [ignore])
AT_CHECK([ovn-nbctl --bare --columns=name list port_group pg1], [0],
[pg1
])

AT_CHECK([ovn-nbctl pg-del pg1], [0], [ignore])
AT_CHECK([ovn-nbctl list port_group], [0], [])

AT_CHECK([ovn-nbctl ls-add sw1], [0], [ignore])
AT_CHECK([ovn-nbctl lsp-add sw1 sw1-p1], [0], [ignore])
SW1P1=$(ovn-nbctl --bare --columns=_uuid list logical_switch_port sw1-p1)
AT_CHECK([ovn-nbctl lsp-add sw1 sw1-p2], [0], [ignore])
SW1P2=$(ovn-nbctl --bare --columns=_uuid list logical_switch_port sw1-p2)

AT_CHECK([ovn-nbctl pg-add pg1 sw1-p1], [0], [ignore])
AT_CHECK([ovn-nbctl --bare --columns=name list port_group pg1], [0],[dnl
pg1
])
AT_CHECK_UNQUOTED([ovn-nbctl --bare --columns=ports list port_group pg1], [0], [dnl
$SW1P1
])

AT_CHECK([ovn-nbctl pg-set-ports pg1 sw1-p2], [0], [ignore])
AT_CHECK_UNQUOTED([ovn-nbctl --bare --columns=ports list port_group pg1], [0], [dnl
$SW1P2
])

AT_CHECK([ovn-nbctl pg-del pg1], [0], [ignore])
AT_CHECK([ovn-nbctl list port_group], [0], [])
])

0 comments on commit 39cc92a

Please sign in to comment.