Remove worthless ebtRules data structure

The ebtRules data structure serves no useful purpose as
the table name is never used and only 1 single chain name
needs to be stored. Just store the chain name directly
in the ebtablesContext instead.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2014-03-07 17:03:51 +00:00
parent 78629cf531
commit 6e69008f3e
2 changed files with 17 additions and 93 deletions

View File

@ -86,57 +86,23 @@ VIR_ONCE_GLOBAL_INIT(virEbTables)
struct _ebtablesContext struct _ebtablesContext
{ {
ebtRules *forward_filter; char *chain;
}; };
enum { enum {
ADD = 0, ADD = 0,
REMOVE, REMOVE,
CREATE,
POLICY,
INSERT
}; };
static void
ebtRulesFree(ebtRules *rules)
{
VIR_FREE(rules->table);
VIR_FREE(rules->chain);
VIR_FREE(rules);
}
static ebtRules *
ebtRulesNew(const char *table,
const char *chain)
{
ebtRules *rules;
if (VIR_ALLOC(rules) < 0)
return NULL;
if (VIR_STRDUP(rules->table, table) < 0)
goto error;
if (VIR_STRDUP(rules->chain, chain) < 0)
goto error;
return rules;
error:
ebtRulesFree(rules);
return NULL;
}
static int ATTRIBUTE_SENTINEL static int ATTRIBUTE_SENTINEL
ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg, ...) ebtablesAddRemoveRule(const char *arg, ...)
{ {
va_list args; va_list args;
int retval = ENOMEM; int retval = ENOMEM;
char **argv; char **argv;
const char *s; const char *s;
int n, command_idx; int n;
n = 1 + /* /sbin/ebtables */ n = 1 + /* /sbin/ebtables */
2 + /* --table foo */ 2 + /* --table foo */
@ -175,16 +141,6 @@ ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg, ...)
if (VIR_STRDUP(argv[n++], EBTABLES_PATH) < 0) if (VIR_STRDUP(argv[n++], EBTABLES_PATH) < 0)
goto error; goto error;
command_idx = n;
if (action == ADD || action == REMOVE) {
if (VIR_STRDUP(argv[n++], "--insert") < 0)
goto error;
if (VIR_STRDUP(argv[n++], rules->chain) < 0)
goto error;
}
if (VIR_STRDUP(argv[n++], arg) < 0) if (VIR_STRDUP(argv[n++], arg) < 0)
goto error; goto error;
@ -199,12 +155,6 @@ ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg, ...)
va_end(args); va_end(args);
if (action == REMOVE) {
VIR_FREE(argv[command_idx]);
if (VIR_STRDUP(argv[command_idx], "--delete") < 0)
goto error;
}
if (virRun((const char **)argv, NULL) < 0) { if (virRun((const char **)argv, NULL) < 0) {
retval = errno; retval = errno;
goto error; goto error;
@ -232,27 +182,14 @@ ebtablesAddRemoveRule(ebtRules *rules, int action, const char *arg, ...)
ebtablesContext * ebtablesContext *
ebtablesContextNew(const char *driver) ebtablesContextNew(const char *driver)
{ {
bool success = false;
ebtablesContext *ctx = NULL; ebtablesContext *ctx = NULL;
char *forward_chain = NULL;
if (VIR_ALLOC(ctx) < 0) if (VIR_ALLOC(ctx) < 0)
return NULL; return NULL;
if (virAsprintf(&forward_chain, "libvirt_%s_FORWARD", driver) < 0) if (virAsprintf(&ctx->chain, "libvirt_%s_FORWARD", driver) < 0) {
goto cleanup; VIR_FREE(ctx);
return NULL;
if (!(ctx->forward_filter = ebtRulesNew("filter", forward_chain)))
goto cleanup;
success = true;
cleanup:
VIR_FREE(forward_chain);
if (!success) {
ebtablesContextFree(ctx);
ctx = NULL;
} }
return ctx; return ctx;
@ -269,8 +206,7 @@ ebtablesContextFree(ebtablesContext *ctx)
{ {
if (!ctx) if (!ctx)
return; return;
if (ctx->forward_filter) VIR_FREE(ctx->chain);
ebtRulesFree(ctx->forward_filter);
VIR_FREE(ctx); VIR_FREE(ctx);
} }
@ -280,19 +216,13 @@ ebtablesForwardPolicyReject(ebtablesContext *ctx,
{ {
/* create it, if it does not exist */ /* create it, if it does not exist */
if (action == ADD) { if (action == ADD) {
ebtablesAddRemoveRule(ctx->forward_filter, ebtablesAddRemoveRule("--new-chain", ctx->chain, NULL,
CREATE,
"--new-chain", ctx->forward_filter->chain, NULL,
NULL); NULL);
ebtablesAddRemoveRule(ctx->forward_filter, ebtablesAddRemoveRule("--insert", "FORWARD", "--jump",
INSERT, ctx->chain, NULL);
"--insert", "FORWARD", "--jump",
ctx->forward_filter->chain, NULL);
} }
return ebtablesAddRemoveRule(ctx->forward_filter, return ebtablesAddRemoveRule("-P", ctx->chain, "DROP",
POLICY,
"-P", ctx->forward_filter->chain, "DROP",
NULL); NULL);
} }
@ -318,12 +248,12 @@ ebtablesForwardAllowIn(ebtablesContext *ctx,
const char *macaddr, const char *macaddr,
int action) int action)
{ {
return ebtablesAddRemoveRule(ctx->forward_filter, return ebtablesAddRemoveRule(action == ADD ? "--insert" : "--delete",
action, ctx->chain,
"--in-interface", iface, "--in-interface", iface,
"--source", macaddr, "--source", macaddr,
"--jump", "ACCEPT", "--jump", "ACCEPT",
NULL); NULL);
} }
/** /**

View File

@ -28,12 +28,6 @@
# include "virmacaddr.h" # include "virmacaddr.h"
typedef struct
{
char *table;
char *chain;
} ebtRules;
typedef struct _ebtablesContext ebtablesContext; typedef struct _ebtablesContext ebtablesContext;
ebtablesContext *ebtablesContextNew (const char *driver); ebtablesContext *ebtablesContextNew (const char *driver);