libvirt/src/conf/netdev_vlan_conf.c
Laine Stump 3f9274a524 conf: add <vlan> element to network and domain interface elements
The following config elements now support a <vlan> subelements:

within a domain: <interface>, and the <actual> subelement of <interface>
within a network: the toplevel, as well as any <portgroup>

Each vlan element must have one or more <tag id='n'/> subelements.  If
there is more than one tag, it is assumed that vlan trunking is being
requested. If trunking is required with only a single tag, the
attribute "trunk='yes'" should be added to the toplevel <vlan>
element.

Some examples:

  <interface type='hostdev'/>
    <vlan>
      <tag id='42'/>
    </vlan>
    <mac address='52:54:00:12:34:56'/>
    ...
  </interface>

  <network>
    <name>vlan-net</name>
    <vlan trunk='yes'>
      <tag id='30'/>
    </vlan>
    <virtualport type='openvswitch'/>
  </network>

  <interface type='network'/>
    <source network='vlan-net'/>
    ...
  </interface>

  <network>
    <name>trunk-vlan</name>
    <vlan>
      <tag id='42'/>
      <tag id='43'/>
    </vlan>
    ...
  </network>

  <network>
    <name>multi</name>
    ...
    <portgroup name='production'/>
      <vlan>
        <tag id='42'/>
      </vlan>
    </portgroup>
    <portgroup name='test'/>
      <vlan>
        <tag id='666'/>
      </vlan>
    </portgroup>
  </network>

  <interface type='network'/>
    <source network='multi' portgroup='test'/>
    ...
  </interface>

IMPORTANT NOTE: As of this patch there is no backend support for the
vlan element for *any* network device type. When support is added in
later patches, it will only be for those select network types that
support setting up a vlan on the host side, without the guest's
involvement. (For example, it will be possible to configure a vlan for
a guest connected to an openvswitch bridge, but it won't be possible
to do that for one that is connected to a standard Linux host bridge.)
2012-08-15 13:10:57 -04:00

132 lines
3.7 KiB
C

/*
* Copyright (C) 2009-2012 Red Hat, Inc.
*
* 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
* License along with this library; If not, see
* <http://www.gnu.org/licenses/>.
*
* Authors:
* Laine Stump <laine@redhat.com>
*/
#include <config.h>
#include "netdev_vlan_conf.h"
#include "virterror_internal.h"
#include "memory.h"
#define VIR_FROM_THIS VIR_FROM_NONE
int
virNetDevVlanParse(xmlNodePtr node, xmlXPathContextPtr ctxt, virNetDevVlanPtr def)
{
int ret = -1;
xmlNodePtr save = ctxt->node;
const char *trunk;
xmlNodePtr *tagNodes = NULL;
int nTags, ii;
ctxt->node = node;
nTags = virXPathNodeSet("./tag", ctxt, &tagNodes);
if (nTags < 0)
goto error;
if (nTags == 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing tag id - each <vlan> must have "
"at least one <tag id='n'/> subelement"));
goto error;
}
if (VIR_ALLOC_N(def->tag, nTags) < 0) {
virReportOOMError();
goto error;
}
for (ii = 0; ii < nTags; ii++) {
unsigned long id;
ctxt->node = tagNodes[ii];
if (virXPathULong("string(./@id)", ctxt, &id) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("missing or invalid vlan tag id attribute"));
goto error;
}
if (id > 4095) {
virReportError(VIR_ERR_XML_ERROR,
_("vlan tag id %lu too large (maximum 4095)"), id);
goto error;
}
def->tag[ii] = id;
}
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);
goto error;
}
/* 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);
goto error;
}
}
}
ret = 0;
error:
ctxt->node = save;
VIR_FREE(tagNodes);
if (ret < 0)
virNetDevVlanClear(def);
return ret;
}
int
virNetDevVlanFormat(virNetDevVlanPtr def, virBufferPtr buf)
{
int ii;
if (def->nTags == 0)
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'" : "");
for (ii = 0; ii < def->nTags; ii++) {
virBufferAsprintf(buf, " <tag id='%u'/>\n", def->tag[ii]);
}
virBufferAddLit(buf, "</vlan>\n");
return 0;
}