Skip to content

Commit

Permalink
dpctl, ovs-ofctl: Unify parsing of ct-flush arguments.
Browse files Browse the repository at this point in the history
In order to make the command extensible unify the arguments
parsing into single function. This will be later on used
for the mark and labels arguments.

Signed-off-by: Ales Musil <[email protected]>
Signed-off-by: 0-day Robot <[email protected]>
  • Loading branch information
almusil authored and ovsrobot committed Dec 6, 2023
1 parent ddf9ef8 commit 1143c54
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 70 deletions.
5 changes: 3 additions & 2 deletions include/openvswitch/ofp-ct.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ bool ofp_ct_tuple_is_zero(const struct ofp_ct_tuple *, uint8_t ip_proto);
bool ofp_ct_tuple_is_five_tuple(const struct ofp_ct_tuple *, uint8_t ip_proto);

void ofp_ct_match_format(struct ds *, const struct ofp_ct_match *);
bool ofp_ct_tuple_parse(struct ofp_ct_tuple *, const char *,
struct ds *, uint8_t *ip_proto, uint16_t *l3_type);
bool ofp_ct_match_parse(const char **, int argc, struct ds *,
struct ofp_ct_match *, bool *with_zone,
uint16_t *zone_id);

enum ofperr ofp_ct_match_decode(struct ofp_ct_match *, bool *with_zone,
uint16_t *zone_id, const struct ofp_header *);
Expand Down
41 changes: 5 additions & 36 deletions lib/dpctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1773,48 +1773,17 @@ dpctl_flush_conntrack(int argc, const char *argv[],
struct dpif *dpif = NULL;
struct ofp_ct_match match = {0};
struct ds ds = DS_EMPTY_INITIALIZER;
uint16_t zone, *pzone = NULL;
uint16_t zone;
int error;
int args = argc - 1;
int zone_pos = 1;
bool with_zone = false;

if (dp_arg_exists(argc, argv)) {
args--;
zone_pos = 2;
}

/* Parse zone. */
if (args && !strncmp(argv[zone_pos], "zone=", 5)) {
if (!ovs_scan(argv[zone_pos], "zone=%"SCNu16, &zone)) {
ds_put_cstr(&ds, "failed to parse zone");
error = EINVAL;
goto error;
}
pzone = &zone;
args--;
}

/* Parse ct tuples. */
for (int i = 0; i < 2; i++) {
if (!args) {
break;
}

struct ofp_ct_tuple *tuple =
i ? &match.tuple_reply : &match.tuple_orig;
const char *arg = argv[argc - args];

if (arg[0] && !ofp_ct_tuple_parse(tuple, arg, &ds, &match.ip_proto,
&match.l3_type)) {
error = EINVAL;
goto error;
}
args--;
}

/* Report error if there is more than one unparsed argument. */
if (args > 0) {
ds_put_cstr(&ds, "invalid arguments");
if (args && !ofp_ct_match_parse(&argv[argc - args], args, &ds, &match,
&with_zone, &zone)) {
error = EINVAL;
goto error;
}
Expand All @@ -1825,7 +1794,7 @@ dpctl_flush_conntrack(int argc, const char *argv[],
return error;
}

error = ct_dpif_flush(dpif, pzone, &match);
error = ct_dpif_flush(dpif, with_zone ? &zone : NULL, &match);
if (!error) {
dpif_close(dpif);
return 0;
Expand Down
47 changes: 46 additions & 1 deletion lib/ofp-ct.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ ofp_ct_match_format(struct ds *ds, const struct ofp_ct_match *match)
/* Parses a specification of a conntrack 5-tuple from 's' into 'tuple'.
* Returns true on success. Otherwise, returns false and puts the error
* message in 'ds'. */
bool
static bool
ofp_ct_tuple_parse(struct ofp_ct_tuple *tuple, const char *s,
struct ds *ds, uint8_t *ip_proto, uint16_t *l3_type)
{
Expand Down Expand Up @@ -219,6 +219,51 @@ ofp_ct_tuple_parse(struct ofp_ct_tuple *tuple, const char *s,
return false;
}

/* Parses a specification of a conntrack match from 'argv' into 'match'.
* Returns true on success. Otherwise, returns false and puts the error
* message in 'ds'. */
bool
ofp_ct_match_parse(const char **argv, int argc, struct ds *ds,
struct ofp_ct_match *match, bool *with_zone,
uint16_t *zone_id)
{
int args = argc;

/* Parse zone. */
if (args && !strncmp(argv[argc - args], "zone=", 5)) {
if (!ovs_scan(argv[argc - args], "zone=%"SCNu16, zone_id)) {
ds_put_cstr(ds, "failed to parse zone");
return false;
}
*with_zone = true;
args--;
}

/* Parse ct tuples. */
for (int i = 0; i < 2; i++) {
if (!args) {
break;
}

struct ofp_ct_tuple *tuple =
i ? &match->tuple_reply : &match->tuple_orig;
const char *arg = argv[argc - args];

if (arg[0] && !ofp_ct_tuple_parse(tuple, arg, ds, &match->ip_proto,
&match->l3_type)) {
return false;
}
args--;
}

if (args > 0) {
ds_put_cstr(ds, "invalid arguments");
return false;
}

return true;
}

static enum ofperr
ofpprop_pull_ipv6(struct ofpbuf *property, struct in6_addr *addr,
uint16_t *l3_type)
Expand Down
2 changes: 1 addition & 1 deletion tests/system-traffic.at
Original file line number Diff line number Diff line change
Expand Up @@ -2859,7 +2859,7 @@ AT_CHECK([ovs-ofctl ct-flush br0 zone=1 'ct_nw_src=10.1.1.1' 'ct_nw_dst=10.1.1.1
AT_CHECK([grep -q "command takes at most 4 arguments" stderr])

AT_CHECK([ovs-ofctl ct-flush br0 'ct_nw_src=10.1.1.1' 'ct_nw_dst=10.1.1.1' invalid], [1], [ignore], [stderr])
AT_CHECK([grep -q "Invalid arguments" stderr])
AT_CHECK([grep -q "invalid arguments" stderr])

OVS_TRAFFIC_VSWITCHD_STOP
AT_CLEANUP
Expand Down
37 changes: 7 additions & 30 deletions utilities/ovs-ofctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3068,42 +3068,19 @@ ofctl_ct_flush(struct ovs_cmdl_context *ctx)
struct vconn *vconn;
struct ofp_ct_match match = {0};
struct ds ds = DS_EMPTY_INITIALIZER;
uint16_t zone, *pzone = NULL;
uint16_t zone;
int args = ctx->argc - 2;
bool with_zone = false;

/* Parse zone. */
if (args && !strncmp(ctx->argv[2], "zone=", 5)) {
if (!ovs_scan(ctx->argv[2], "zone=%"SCNu16, &zone)) {
ovs_fatal(0, "Failed to parse zone");
}
pzone = &zone;
args--;
}

/* Parse ct tuples. */
for (int i = 0; i < 2; i++) {
if (!args) {
break;
}

struct ofp_ct_tuple *tuple =
i ? &match.tuple_reply : &match.tuple_orig;
const char *arg = ctx->argv[ctx->argc - args];

if (arg[0] && !ofp_ct_tuple_parse(tuple, arg, &ds, &match.ip_proto,
&match.l3_type)) {
ovs_fatal(0, "Failed to parse ct-tuple: %s", ds_cstr(&ds));
}
args--;
}

if (args > 0) {
ovs_fatal(0, "Invalid arguments");
if (args && !ofp_ct_match_parse((const char **) &ctx->argv[2],
args, &ds, &match, &with_zone, &zone)) {
ovs_fatal(0, "Failed to parse CT match: %s", ds_cstr(&ds));
}

open_vconn(ctx->argv[1], &vconn);
enum ofp_version version = vconn_get_version(vconn);
struct ofpbuf *msg = ofp_ct_match_encode(&match, pzone, version);
struct ofpbuf *msg =
ofp_ct_match_encode(&match, with_zone ? &zone : NULL, version);

ds_destroy(&ds);
transact_noreply(vconn, msg);
Expand Down

0 comments on commit 1143c54

Please sign in to comment.