2012-08-12 07:51:30 +00:00
|
|
|
/*
|
2014-02-20 11:59:55 +00:00
|
|
|
* Copyright (C) 2009-2014 Red Hat, Inc.
|
2012-08-12 07:51:30 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-08-12 07:51:30 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "netdev_vlan_conf.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-08-12 07:51:30 +00:00
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virNativeVlanMode,
|
|
|
|
VIR_NATIVE_VLAN_MODE_LAST,
|
2019-01-20 16:30:15 +00:00
|
|
|
"default", "tagged", "untagged",
|
|
|
|
);
|
2013-05-23 17:12:10 +00:00
|
|
|
|
2012-08-12 07:51:30 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlan *def)
|
2012-08-12 07:51:30 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2020-07-28 19:47:48 +00:00
|
|
|
VIR_XPATH_NODE_AUTORESTORE(ctxt)
|
2013-09-03 11:36:22 +00:00
|
|
|
char *trunk = NULL;
|
|
|
|
char *nativeMode = NULL;
|
2012-08-12 07:51:30 +00:00
|
|
|
xmlNodePtr *tagNodes = NULL;
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int nTags;
|
|
|
|
size_t i;
|
2012-08-12 07:51:30 +00:00
|
|
|
|
|
|
|
ctxt->node = node;
|
|
|
|
|
|
|
|
nTags = virXPathNodeSet("./tag", ctxt, &tagNodes);
|
|
|
|
if (nTags < 0)
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2012-08-12 07:51:30 +00:00
|
|
|
|
|
|
|
if (nTags == 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("missing tag id - each <vlan> must have "
|
|
|
|
"at least one <tag id='n'/> subelement"));
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-07 19:15:50 +00:00
|
|
|
def->tag = g_new0(unsigned int, nTags);
|
2013-05-23 17:12:10 +00:00
|
|
|
def->nativeMode = 0;
|
|
|
|
def->nativeTag = 0;
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
for (i = 0; i < nTags; i++) {
|
2012-08-12 07:51:30 +00:00
|
|
|
unsigned long id;
|
|
|
|
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
ctxt->node = tagNodes[i];
|
2012-08-12 07:51:30 +00:00
|
|
|
if (virXPathULong("string(./@id)", ctxt, &id) < 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("missing or invalid vlan tag id attribute"));
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
|
|
|
if (id > 4095) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("vlan tag id %lu too large (maximum 4095)"), id);
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
2013-05-23 17:12:10 +00:00
|
|
|
if ((nativeMode = virXPathString("string(./@nativeMode)", ctxt))) {
|
|
|
|
if (def->nativeMode != 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("duplicate native vlan setting"));
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2013-05-23 17:12:10 +00:00
|
|
|
}
|
|
|
|
if ((def->nativeMode
|
|
|
|
= virNativeVlanModeTypeFromString(nativeMode)) <= 0) {
|
2014-01-10 16:41:33 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
2013-05-23 17:12:10 +00:00
|
|
|
_("Invalid \"nativeMode='%s'\" "
|
|
|
|
"in vlan <tag> element"),
|
|
|
|
nativeMode);
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2013-05-23 17:12:10 +00:00
|
|
|
}
|
2013-06-28 16:04:37 +00:00
|
|
|
VIR_FREE(nativeMode);
|
2013-05-23 17:12:10 +00:00
|
|
|
def->nativeTag = id;
|
|
|
|
}
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
def->tag[i] = id;
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def->nTags = nTags;
|
|
|
|
|
|
|
|
/* now that we know how many tags there are, look for an explicit
|
|
|
|
* trunk setting.
|
|
|
|
*/
|
|
|
|
if (nTags > 1)
|
|
|
|
def->trunk = true;
|
|
|
|
|
|
|
|
ctxt->node = node;
|
|
|
|
if ((trunk = virXPathString("string(./@trunk)", ctxt)) != NULL) {
|
|
|
|
def->trunk = STRCASEEQ(trunk, "yes");
|
|
|
|
if (!def->trunk) {
|
|
|
|
if (nTags > 1) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("invalid \"trunk='%s'\" in <vlan> - trunk='yes' "
|
|
|
|
"is required for more than one vlan tag"), trunk);
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
2013-05-23 17:12:10 +00:00
|
|
|
if (def->nativeMode != 0) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("invalid configuration in <vlan> - \"trunk='no'\" is "
|
|
|
|
"not allowed with a native vlan id"));
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2013-05-23 17:12:10 +00:00
|
|
|
}
|
2012-08-12 07:51:30 +00:00
|
|
|
/* allow (but discard) "trunk='no' if there is a single tag */
|
|
|
|
if (STRCASENEQ(trunk, "no")) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("invalid \"trunk='%s'\" in <vlan> "
|
|
|
|
"- must be yes or no"), trunk);
|
2013-06-28 16:04:37 +00:00
|
|
|
goto cleanup;
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:48:31 +00:00
|
|
|
cleanup:
|
2012-08-12 07:51:30 +00:00
|
|
|
VIR_FREE(tagNodes);
|
2013-02-04 14:57:00 +00:00
|
|
|
VIR_FREE(trunk);
|
2013-06-28 16:04:37 +00:00
|
|
|
VIR_FREE(nativeMode);
|
2012-08-12 07:51:30 +00:00
|
|
|
if (ret < 0)
|
|
|
|
virNetDevVlanClear(def);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virNetDevVlanFormat(const virNetDevVlan *def, virBuffer *buf)
|
2012-08-12 07:51:30 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-08-12 07:51:30 +00:00
|
|
|
|
2014-02-20 11:59:55 +00:00
|
|
|
if (!(def && def->nTags))
|
2012-08-12 07:51:30 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!def->tag) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("missing vlan tag data"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
virBufferAsprintf(buf, "<vlan%s>\n", def->trunk ? " trunk='yes'" : "");
|
2014-03-06 14:58:56 +00:00
|
|
|
virBufferAdjustIndent(buf, 2);
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
for (i = 0; i < def->nTags; i++) {
|
2013-05-23 17:12:10 +00:00
|
|
|
if (def->nativeMode != VIR_NATIVE_VLAN_MODE_DEFAULT &&
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
def->nativeTag == def->tag[i]) {
|
2013-05-23 17:12:10 +00:00
|
|
|
/* check the nativeMode in case we get <tag id='0'/>*/
|
|
|
|
const char *mode = virNativeVlanModeTypeToString(def->nativeMode);
|
|
|
|
if (!mode) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Bad value for nativeMode"));
|
|
|
|
}
|
2014-03-06 14:58:56 +00:00
|
|
|
virBufferAsprintf(buf, "<tag id='%u' nativeMode='%s'/>\n",
|
Convert 'int i' to 'size_t i' in src/conf/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
def->tag[i], mode);
|
2013-05-23 17:12:10 +00:00
|
|
|
} else {
|
2014-03-06 14:58:56 +00:00
|
|
|
virBufferAsprintf(buf, "<tag id='%u'/>\n", def->tag[i]);
|
2013-05-23 17:12:10 +00:00
|
|
|
}
|
2012-08-12 07:51:30 +00:00
|
|
|
}
|
2014-03-06 14:58:56 +00:00
|
|
|
virBufferAdjustIndent(buf, -2);
|
2012-08-12 07:51:30 +00:00
|
|
|
virBufferAddLit(buf, "</vlan>\n");
|
|
|
|
return 0;
|
|
|
|
}
|