[libvirt] [PATCH 9/9] add DHCP snooping support to nwfilter
by David L Stevens
This patch removes remaining pieces of IP address learning.
diff --git a/src/Makefile.am b/src/Makefile.am
index 3da0797..53cdc00 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -389,9 +389,7 @@ NWFILTER_DRIVER_SOURCES = \
nwfilter/nwfilter_dhcpsnoop.c \
nwfilter/nwfilter_dhcpsnoop.h \
nwfilter/nwfilter_ebiptables_driver.c \
- nwfilter/nwfilter_ebiptables_driver.h \
- nwfilter/nwfilter_learnipaddr.c \
- nwfilter/nwfilter_learnipaddr.h
+ nwfilter/nwfilter_ebiptables_driver.h
# Security framework and drivers for various models
diff --git a/src/nwfilter/nwfilter_driver.c b/src/nwfilter/nwfilter_driver.c
index 2e20e59..3a73fa4 100644
--- a/src/nwfilter/nwfilter_driver.c
+++ b/src/nwfilter/nwfilter_driver.c
@@ -40,7 +40,6 @@
#include "configmake.h"
#include "nwfilter_dhcpsnoop.h"
-#include "nwfilter_learnipaddr.h"
#define VIR_FROM_THIS VIR_FROM_NWFILTER
@@ -69,8 +68,6 @@ nwfilterDriverStartup(int privileged) {
if (virNWFilterDHCPSnoopInit() < 0)
return -1;
- if (virNWFilterLearnInit() < 0)
- return -1;
virNWFilterTechDriversInit(privileged);
@@ -131,7 +128,6 @@ alloc_err_exit:
conf_init_err:
virNWFilterTechDriversShutdown();
virNWFilterDHCPSnoopEnd(0);
- virNWFilterLearnShutdown();
return -1;
}
@@ -154,7 +150,7 @@ nwfilterDriverReload(void) {
if (conn) {
/* shut down all threads -- they will be restarted if necessary */
- virNWFilterLearnThreadsTerminate(true);
+ virNWFilterDHCPSnoopEnd(0);
nwfilterDriverLock(driverState);
virNWFilterCallbackDriversLock();
@@ -206,7 +202,6 @@ nwfilterDriverShutdown(void) {
virNWFilterConfLayerShutdown();
virNWFilterTechDriversShutdown();
virNWFilterDHCPSnoopEnd(0);
- virNWFilterLearnShutdown();
nwfilterDriverLock(driverState);
diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c
index c6e6600..42fd965 100644
--- a/src/nwfilter/nwfilter_gentech_driver.c
+++ b/src/nwfilter/nwfilter_gentech_driver.c
@@ -34,7 +34,6 @@
#include "nwfilter_gentech_driver.h"
#include "nwfilter_ebiptables_driver.h"
#include "nwfilter_dhcpsnoop.h"
-#include "nwfilter_learnipaddr.h"
#define VIR_FROM_THIS VIR_FROM_NWFILTER
@@ -625,14 +624,10 @@ virNWFilterChangeVar(virConnectPtr conn,
return 1;
}
- if (virNWFilterLockIface(ifname))
- goto err_exit;
-
if (delete)
rc = techdriver->removeRules(conn, ifname, nptrs, ptrs);
else
rc = techdriver->addRules(conn, ifname, nptrs, ptrs);
- virNWFilterUnlockIface(ifname);
VIR_FREE(ptrs);
err_exit:
@@ -755,9 +750,6 @@ virNWFilterInstantiate(virConnectPtr conn,
if (rc)
goto err_exit;
- if (virNWFilterLockIface(ifname))
- goto err_exit;
-
rc = techdriver->applyNewRules(conn, ifname, nptrs, ptrs);
if (teardownOld && rc == 0)
@@ -768,8 +760,6 @@ virNWFilterInstantiate(virConnectPtr conn,
techdriver->allTeardown(ifname);
rc = 1;
}
-
- virNWFilterUnlockIface(ifname);
}
err_exit:
@@ -811,7 +801,6 @@ __virNWFilterInstantiateFilter(virConnectPtr conn,
virNWFilterDefPtr filter;
char vmmacaddr[VIR_MAC_STRING_BUFLEN] = {0};
char *str_macaddr = NULL;
- const char *ipaddr;
char *str_ipaddr = NULL;
techdriver = virNWFilterTechDriverForName(drvname);
@@ -850,16 +839,6 @@ __virNWFilterInstantiateFilter(virConnectPtr conn,
goto err_exit;
}
- ipaddr = virNWFilterGetIpAddrForIfname(ifname);
- if (ipaddr) {
- str_ipaddr = strdup(ipaddr);
- if (!str_ipaddr) {
- virReportOOMError();
- rc = 1;
- goto err_exit;
- }
- }
-
vars1 = virNWFilterCreateVarHashmap(str_macaddr, str_ipaddr);
if (!vars1) {
rc = 1;
@@ -1031,7 +1010,6 @@ int virNWFilterRollbackUpdateFilter(virConnectPtr conn,
const virDomainNetDefPtr net)
{
const char *drvname = EBIPTABLES_DRIVER_ID;
- int ifindex;
virNWFilterTechDriverPtr techdriver;
techdriver = virNWFilterTechDriverForName(drvname);
@@ -1043,11 +1021,6 @@ int virNWFilterRollbackUpdateFilter(virConnectPtr conn,
return 1;
}
- /* don't tear anything while the address is being learned */
- if (ifaceGetIndex(true, net->ifname, &ifindex) == 0 &&
- virNWFilterLookupLearnReq(ifindex) != NULL)
- return 0;
-
return techdriver->tearNewRules(conn, net->ifname);
}
@@ -1057,7 +1030,6 @@ virNWFilterTearOldFilter(virConnectPtr conn,
virDomainNetDefPtr net)
{
const char *drvname = EBIPTABLES_DRIVER_ID;
- int ifindex;
virNWFilterTechDriverPtr techdriver;
techdriver = virNWFilterTechDriverForName(drvname);
@@ -1069,11 +1041,6 @@ virNWFilterTearOldFilter(virConnectPtr conn,
return 1;
}
- /* don't tear anything while the address is being learned */
- if (ifaceGetIndex(true, net->ifname, &ifindex) == 0 &&
- virNWFilterLookupLearnReq(ifindex) != NULL)
- return 0;
-
return techdriver->tearOldRules(conn, net->ifname);
}
@@ -1095,17 +1062,8 @@ _virNWFilterTeardownFilter(const char *ifname)
virNWFilterDHCPSnoopEnd(ifname);
- virNWFilterTerminateLearnReq(ifname);
-
- if (virNWFilterLockIface(ifname))
- return 1;
-
techdriver->allTeardown(ifname);
- virNWFilterDelIpAddrForIfname(ifname);
-
- virNWFilterUnlockIface(ifname);
-
return 0;
}
diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c
deleted file mode 100644
index 96d2a55..0000000
--- a/src/nwfilter/nwfilter_learnipaddr.c
+++ /dev/null
@@ -1,891 +0,0 @@
-/*
- * nwfilter_learnipaddr.c: support for learning IP address used by a VM
- * on an interface
- *
- * Copyright (C) 2011 Red Hat, Inc.
- * Copyright (C) 2010 IBM Corp.
- * Copyright (C) 2010 Stefan Berger
- *
- * 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Stefan Berger <stefanb(a)us.ibm.com>
- */
-
-#include <config.h>
-
-#ifdef HAVE_LIBPCAP
-# include <pcap.h>
-#endif
-
-#include <fcntl.h>
-#include <sys/ioctl.h>
-
-#include <arpa/inet.h>
-#include <net/ethernet.h>
-#include <netinet/ip.h>
-#include <netinet/udp.h>
-#include <net/if_arp.h>
-#include <intprops.h>
-
-#include "internal.h"
-
-#include "buf.h"
-#include "memory.h"
-#include "logging.h"
-#include "datatypes.h"
-#include "interface.h"
-#include "virterror_internal.h"
-#include "threads.h"
-#include "conf/nwfilter_params.h"
-#include "conf/domain_conf.h"
-#include "nwfilter_gentech_driver.h"
-#include "nwfilter_ebiptables_driver.h"
-#include "nwfilter_learnipaddr.h"
-
-#define VIR_FROM_THIS VIR_FROM_NWFILTER
-
-#define IFINDEX2STR(VARNAME, ifindex) \
- char VARNAME[INT_BUFSIZE_BOUND(ifindex)]; \
- snprintf(VARNAME, sizeof(VARNAME), "%d", ifindex);
-
-#define PKT_TIMEOUT_MS 500 /* ms */
-
-/* structure of an ARP request/reply message */
-struct f_arphdr {
- struct arphdr arphdr;
- uint8_t ar_sha[ETH_ALEN];
- uint32_t ar_sip;
- uint8_t ar_tha[ETH_ALEN];
- uint32_t ar_tip;
-} ATTRIBUTE_PACKED;
-
-
-struct dhcp_option {
- uint8_t code;
- uint8_t len;
- uint8_t value[0]; /* length varies */
-} ATTRIBUTE_PACKED;
-
-
-/* structure representing DHCP message */
-struct dhcp {
- uint8_t op;
- uint8_t htype;
- uint8_t hlen;
- uint8_t hops;
- uint32_t xid;
- uint16_t secs;
- uint16_t flags;
- uint32_t ciaddr;
- uint32_t yiaddr;
- uint32_t siaddr;
- uint32_t giaddr;
- uint8_t chaddr[16];
- uint8_t zeroes[192];
- uint32_t magic;
- struct dhcp_option options[0];
-} ATTRIBUTE_PACKED;
-
-#define DHCP_MSGT_DHCPOFFER 2
-#define DHCP_MSGT_DHCPACK 5
-
-
-#define DHCP_OPT_BCASTADDRESS 28
-#define DHCP_OPT_MESSAGETYPE 53
-
-struct ether_vlan_header
-{
- uint8_t dhost[ETH_ALEN];
- uint8_t shost[ETH_ALEN];
- uint16_t vlan_type;
- uint16_t vlan_flags;
- uint16_t ether_type;
-} ATTRIBUTE_PACKED;
-
-
-static virMutex pendingLearnReqLock;
-static virHashTablePtr pendingLearnReq;
-
-static virMutex ipAddressMapLock;
-static virNWFilterHashTablePtr ipAddressMap;
-
-static virMutex ifaceMapLock;
-static virHashTablePtr ifaceLockMap;
-
-typedef struct _virNWFilterIfaceLock virNWFilterIfaceLock;
-typedef virNWFilterIfaceLock *virNWFilterIfaceLockPtr;
-struct _virNWFilterIfaceLock {
- char ifname[IF_NAMESIZE];
- virMutex lock;
- int refctr;
-};
-
-
-static bool threadsTerminate = false;
-
-
-int
-virNWFilterLockIface(const char *ifname) {
- virNWFilterIfaceLockPtr ifaceLock;
-
- virMutexLock(&ifaceMapLock);
-
- ifaceLock = virHashLookup(ifaceLockMap, ifname);
- if (!ifaceLock) {
- if (VIR_ALLOC(ifaceLock) < 0) {
- virReportOOMError();
- goto err_exit;
- }
-
- if (virMutexInitRecursive(&ifaceLock->lock)) {
- virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("mutex initialization failed"));
- VIR_FREE(ifaceLock);
- goto err_exit;
- }
-
- if (virStrcpyStatic(ifaceLock->ifname, ifname) == NULL) {
- virNWFilterReportError(VIR_ERR_INTERNAL_ERROR,
- _("interface name %s does not fit into "
- "buffer "),
- ifaceLock->ifname);
- VIR_FREE(ifaceLock);
- goto err_exit;
- }
-
- while (virHashAddEntry(ifaceLockMap, ifname, ifaceLock)) {
- VIR_FREE(ifaceLock);
- goto err_exit;
- }
-
- ifaceLock->refctr = 0;
- }
-
- ifaceLock->refctr++;
-
- virMutexUnlock(&ifaceMapLock);
-
- virMutexLock(&ifaceLock->lock);
-
- return 0;
-
- err_exit:
- virMutexUnlock(&ifaceMapLock);
-
- return 1;
-}
-
-
-static void
-freeIfaceLock(void *payload, const void *name ATTRIBUTE_UNUSED) {
- VIR_FREE(payload);
-}
-
-
-void
-virNWFilterUnlockIface(const char *ifname) {
- virNWFilterIfaceLockPtr ifaceLock;
-
- virMutexLock(&ifaceMapLock);
-
- ifaceLock = virHashLookup(ifaceLockMap, ifname);
-
- if (ifaceLock) {
- virMutexUnlock(&ifaceLock->lock);
-
- ifaceLock->refctr--;
- if (ifaceLock->refctr == 0)
- virHashRemoveEntry(ifaceLockMap, ifname);
- }
-
- virMutexUnlock(&ifaceMapLock);
-}
-
-
-static void
-virNWFilterIPAddrLearnReqFree(virNWFilterIPAddrLearnReqPtr req) {
- if (!req)
- return;
-
- VIR_FREE(req->filtername);
- virNWFilterHashTableFree(req->filterparams);
-
- VIR_FREE(req);
-}
-
-
-#if HAVE_LIBPCAP
-
-static int
-virNWFilterRegisterLearnReq(virNWFilterIPAddrLearnReqPtr req) {
- int res = -1;
- IFINDEX2STR(ifindex_str, req->ifindex);
-
- virMutexLock(&pendingLearnReqLock);
-
- if (!virHashLookup(pendingLearnReq, ifindex_str))
- res = virHashAddEntry(pendingLearnReq, ifindex_str, req);
-
- virMutexUnlock(&pendingLearnReqLock);
-
- return res;
-}
-
-
-#endif
-
-int
-virNWFilterTerminateLearnReq(const char *ifname) {
- int rc = 1;
- int ifindex;
- virNWFilterIPAddrLearnReqPtr req;
-
- if (ifaceGetIndex(false, ifname, &ifindex) == 0) {
-
- IFINDEX2STR(ifindex_str, ifindex);
-
- virMutexLock(&pendingLearnReqLock);
-
- req = virHashLookup(pendingLearnReq, ifindex_str);
- if (req) {
- rc = 0;
- req->terminate = true;
- }
-
- virMutexUnlock(&pendingLearnReqLock);
- }
-
- return rc;
-}
-
-
-virNWFilterIPAddrLearnReqPtr
-virNWFilterLookupLearnReq(int ifindex) {
- void *res;
- IFINDEX2STR(ifindex_str, ifindex);
-
- virMutexLock(&pendingLearnReqLock);
-
- res = virHashLookup(pendingLearnReq, ifindex_str);
-
- virMutexUnlock(&pendingLearnReqLock);
-
- return res;
-}
-
-
-static void
-freeLearnReqEntry(void *payload, const void *name ATTRIBUTE_UNUSED) {
- virNWFilterIPAddrLearnReqFree(payload);
-}
-
-
-#ifdef HAVE_LIBPCAP
-
-static virNWFilterIPAddrLearnReqPtr
-virNWFilterDeregisterLearnReq(int ifindex) {
- virNWFilterIPAddrLearnReqPtr res;
- IFINDEX2STR(ifindex_str, ifindex);
-
- virMutexLock(&pendingLearnReqLock);
-
- res = virHashSteal(pendingLearnReq, ifindex_str);
-
- virMutexUnlock(&pendingLearnReqLock);
-
- return res;
-}
-
-
-
-static int
-virNWFilterAddIpAddrForIfname(const char *ifname, char *addr) {
- int ret;
-
- virMutexLock(&ipAddressMapLock);
-
- ret = virNWFilterHashTablePut(ipAddressMap, ifname, addr, 1);
-
- virMutexUnlock(&ipAddressMapLock);
-
- return ret;
-}
-#endif
-
-
-void
-virNWFilterDelIpAddrForIfname(const char *ifname) {
-
- virMutexLock(&ipAddressMapLock);
-
- if (virHashLookup(ipAddressMap->hashTable, ifname))
- virNWFilterHashTableRemoveEntry(ipAddressMap, ifname);
-
- virMutexUnlock(&ipAddressMapLock);
-}
-
-
-const char *
-virNWFilterGetIpAddrForIfname(const char *ifname) {
- const char *res;
-
- virMutexLock(&ipAddressMapLock);
-
- res = virHashLookup(ipAddressMap->hashTable, ifname);
-
- virMutexUnlock(&ipAddressMapLock);
-
- return res;
-}
-
-
-#ifdef HAVE_LIBPCAP
-
-static void
-procDHCPOpts(struct dhcp *dhcp, int dhcp_opts_len,
- uint32_t *vmaddr, uint32_t *bcastaddr,
- enum howDetect *howDetected) {
- struct dhcp_option *dhcpopt = &dhcp->options[0];
-
- while (dhcp_opts_len >= 2) {
-
- switch (dhcpopt->code) {
-
- case DHCP_OPT_BCASTADDRESS: /* Broadcast address */
- if (dhcp_opts_len >= 6) {
- uint32_t *tmp = (uint32_t *)&dhcpopt->value;
- (*bcastaddr) = ntohl(*tmp);
- }
- break;
-
- case DHCP_OPT_MESSAGETYPE: /* Message type */
- if (dhcp_opts_len >= 3) {
- uint8_t *val = (uint8_t *)&dhcpopt->value;
- switch (*val) {
- case DHCP_MSGT_DHCPACK:
- case DHCP_MSGT_DHCPOFFER:
- *vmaddr = dhcp->yiaddr;
- *howDetected = DETECT_DHCP;
- break;
- }
- }
- }
- dhcp_opts_len -= (2 + dhcpopt->len);
- dhcpopt = (struct dhcp_option*)((char *)dhcpopt + 2 + dhcpopt->len);
- }
-}
-
-
-/**
- * learnIPAddressThread
- * arg: pointer to virNWFilterIPAddrLearnReq structure
- *
- * Learn the IP address being used on an interface. Use ARP Request and
- * Reply messages, DHCP offers and the first IP packet being sent from
- * the VM to detect the IP address it is using. Detects only one IP address
- * per interface (IP aliasing not supported). The method on how the
- * IP address is detected can be chosen through flags. DETECT_DHCP will
- * require that the IP address is detected from a DHCP OFFER, DETECT_STATIC
- * will require that the IP address was taken from an ARP packet or an IPv4
- * packet. Both flags can be set at the same time.
- */
-static void *
-learnIPAddressThread(void *arg)
-{
- char errbuf[PCAP_ERRBUF_SIZE] = {0};
- pcap_t *handle = NULL;
- struct bpf_program fp;
- struct pcap_pkthdr header;
- const u_char *packet;
- struct ether_header *ether_hdr;
- struct ether_vlan_header *vlan_hdr;
- virNWFilterIPAddrLearnReqPtr req = arg;
- uint32_t vmaddr = 0, bcastaddr = 0;
- unsigned int ethHdrSize;
- char *listen_if = (strlen(req->linkdev) != 0) ? req->linkdev
- : req->ifname;
- int dhcp_opts_len;
- char macaddr[VIR_MAC_STRING_BUFLEN];
- virBuffer buf = VIR_BUFFER_INITIALIZER;
- char *filter = NULL;
- uint16_t etherType;
- bool showError = true;
- enum howDetect howDetected = 0;
- virNWFilterTechDriverPtr techdriver = req->techdriver;
-
- if (virNWFilterLockIface(req->ifname))
- goto err_no_lock;
-
- req->status = 0;
-
- /* anything change to the VM's interface -- check at least once */
- if (ifaceCheck(false, req->ifname, NULL, req->ifindex)) {
- req->status = ENODEV;
- goto done;
- }
-
- handle = pcap_open_live(listen_if, BUFSIZ, 0, PKT_TIMEOUT_MS, errbuf);
-
- if (handle == NULL) {
- VIR_DEBUG("Couldn't open device %s: %s\n", listen_if, errbuf);
- req->status = ENODEV;
- goto done;
- }
-
- virFormatMacAddr(req->macaddr, macaddr);
-
- switch (req->howDetect) {
- case DETECT_DHCP:
- if (techdriver->applyDHCPOnlyRules(req->ifname,
- req->macaddr,
- NULL)) {
- req->status = EINVAL;
- goto done;
- }
- virBufferVSprintf(&buf, " ether dst %s"
- " and src port 67 and dst port 68",
- macaddr);
- break;
- default:
- if (techdriver->applyBasicRules(req->ifname,
- req->macaddr)) {
- req->status = EINVAL;
- goto done;
- }
- virBufferVSprintf(&buf, "ether host %s", macaddr);
- }
-
- if (virBufferError(&buf)) {
- req->status = ENOMEM;
- goto done;
- }
-
- filter = virBufferContentAndReset(&buf);
-
- if (pcap_compile(handle, &fp, filter, 1, 0) != 0) {
- VIR_DEBUG("Couldn't compile filter '%s'.\n", filter);
- req->status = EINVAL;
- goto done;
- }
-
- if (pcap_setfilter(handle, &fp) != 0) {
- VIR_DEBUG("Couldn't set filter '%s'.\n", filter);
- req->status = EINVAL;
- pcap_freecode(&fp);
- goto done;
- }
-
- pcap_freecode(&fp);
-
- while (req->status == 0 && vmaddr == 0) {
- packet = pcap_next(handle, &header);
-
- if (!packet) {
-
- if (threadsTerminate || req->terminate) {
- req->status = ECANCELED;
- showError = false;
- break;
- }
-
- /* check whether VM's dev is still there */
- if (ifaceCheck(false, req->ifname, NULL, req->ifindex)) {
- req->status = ENODEV;
- showError = false;
- break;
- }
- continue;
- }
-
- if (header.len >= sizeof(struct ether_header)) {
- ether_hdr = (struct ether_header*)packet;
-
- switch (ntohs(ether_hdr->ether_type)) {
-
- case ETHERTYPE_IP:
- ethHdrSize = sizeof(struct ether_header);
- etherType = ntohs(ether_hdr->ether_type);
- break;
-
- case ETHERTYPE_VLAN:
- ethHdrSize = sizeof(struct ether_vlan_header);
- vlan_hdr = (struct ether_vlan_header *)packet;
- if (ntohs(vlan_hdr->ether_type) != ETHERTYPE_IP ||
- header.len < ethHdrSize)
- continue;
- etherType = ntohs(vlan_hdr->ether_type);
- break;
-
- default:
- continue;
- }
-
- if (memcmp(ether_hdr->ether_shost,
- req->macaddr,
- VIR_MAC_BUFLEN) == 0) {
- /* packets from the VM */
-
- if (etherType == ETHERTYPE_IP &&
- (header.len >= ethHdrSize +
- sizeof(struct iphdr))) {
- struct iphdr *iphdr = (struct iphdr*)(packet +
- ethHdrSize);
- vmaddr = iphdr->saddr;
- /* skip mcast addresses (224.0.0.0 - 239.255.255.255),
- * class E (240.0.0.0 - 255.255.255.255, includes eth.
- * bcast) and zero address in DHCP Requests */
- if ( (ntohl(vmaddr) & 0xe0000000) == 0xe0000000 ||
- vmaddr == 0) {
- vmaddr = 0;
- continue;
- }
-
- howDetected = DETECT_STATIC;
- } else if (etherType == ETHERTYPE_ARP &&
- (header.len >= ethHdrSize +
- sizeof(struct f_arphdr))) {
- struct f_arphdr *arphdr = (struct f_arphdr*)(packet +
- ethHdrSize);
- switch (ntohs(arphdr->arphdr.ar_op)) {
- case ARPOP_REPLY:
- vmaddr = arphdr->ar_sip;
- howDetected = DETECT_STATIC;
- break;
- case ARPOP_REQUEST:
- vmaddr = arphdr->ar_tip;
- howDetected = DETECT_STATIC;
- break;
- }
- }
- } else if (memcmp(ether_hdr->ether_dhost,
- req->macaddr,
- VIR_MAC_BUFLEN) == 0) {
- /* packets to the VM */
- if (etherType == ETHERTYPE_IP &&
- (header.len >= ethHdrSize +
- sizeof(struct iphdr))) {
- struct iphdr *iphdr = (struct iphdr*)(packet +
- ethHdrSize);
- if ((iphdr->protocol == IPPROTO_UDP) &&
- (header.len >= ethHdrSize +
- iphdr->ihl * 4 +
- sizeof(struct udphdr))) {
- struct udphdr *udphdr= (struct udphdr *)
- ((char *)iphdr + iphdr->ihl * 4);
- if (ntohs(udphdr->source) == 67 &&
- ntohs(udphdr->dest) == 68 &&
- header.len >= ethHdrSize +
- iphdr->ihl * 4 +
- sizeof(struct udphdr) +
- sizeof(struct dhcp)) {
- struct dhcp *dhcp = (struct dhcp *)
- ((char *)udphdr + sizeof(udphdr));
- if (dhcp->op == 2 /* BOOTREPLY */ &&
- !memcmp(&dhcp->chaddr[0],
- req->macaddr,
- 6)) {
- dhcp_opts_len = header.len -
- (ethHdrSize + iphdr->ihl * 4 +
- sizeof(struct udphdr) +
- sizeof(struct dhcp));
- procDHCPOpts(dhcp, dhcp_opts_len,
- &vmaddr,
- &bcastaddr,
- &howDetected);
- }
- }
- }
- }
- }
- }
- if (vmaddr && (req->howDetect & howDetected) == 0) {
- vmaddr = 0;
- howDetected = 0;
- }
- } /* while */
-
- done:
- VIR_FREE(filter);
-
- if (handle)
- pcap_close(handle);
-
- if (req->status == 0) {
- int ret;
- virSocketAddr sa;
- sa.len = sizeof(sa.data.inet4);
- sa.data.inet4.sin_family = AF_INET;
- sa.data.inet4.sin_addr.s_addr = vmaddr;
- char *inetaddr;
-
- if ((inetaddr = virSocketFormatAddr(&sa))!= NULL) {
- virNWFilterAddIpAddrForIfname(req->ifname, inetaddr);
-
- ret = virNWFilterInstantiateFilterLate(NULL,
- req->ifname,
- req->ifindex,
- req->linkdev,
- req->nettype,
- req->macaddr,
- req->filtername,
- req->filterparams,
- req->driver);
- VIR_DEBUG("Result from applying firewall rules on "
- "%s with IP addr %s : %d\n", req->ifname, inetaddr, ret);
- }
- } else {
- if (showError)
- virReportSystemError(req->status,
- _("encountered an error on interface %s "
- "index %d"),
- req->ifname, req->ifindex);
-
- techdriver->applyDropAllRules(req->ifname);
- }
-
- memset(&req->thread, 0x0, sizeof(req->thread));
-
- VIR_DEBUG("pcap thread terminating for interface %s\n",req->ifname);
-
- virNWFilterUnlockIface(req->ifname);
-
- err_no_lock:
- virNWFilterDeregisterLearnReq(req->ifindex);
-
- virNWFilterIPAddrLearnReqFree(req);
-
- return 0;
-}
-
-
-/**
- * virNWFilterLearnIPAddress
- * @techdriver : driver to build firewalls
- * @ifname: the name of the interface
- * @ifindex: the index of the interface
- * @linkdev : the name of the link device; currently only used in case of a
- * macvtap device
- * @nettype : the type of interface
- * @macaddr : the MAC address of the interface
- * @filtername : the name of the top-level filter to apply to the interface
- * once its IP address has been detected
- * @driver : the network filter driver
- * @howDetect : the method on how the thread is supposed to detect the
- * IP address; must choose any of the available flags
- *
- * Instruct to learn the IP address being used on a given interface (ifname).
- * Unless there already is a thread attempting to learn the IP address
- * being used on the interface, a thread is started that will listen on
- * the traffic being sent on the interface (or link device) with the
- * MAC address that is provided. Will then launch the application of the
- * firewall rules on the interface.
- */
-int
-virNWFilterLearnIPAddress(virNWFilterTechDriverPtr techdriver,
- const char *ifname,
- int ifindex,
- const char *linkdev,
- enum virDomainNetType nettype,
- const unsigned char *macaddr,
- const char *filtername,
- virNWFilterHashTablePtr filterparams,
- virNWFilterDriverStatePtr driver,
- enum howDetect howDetect) {
- int rc;
- virNWFilterIPAddrLearnReqPtr req = NULL;
- virNWFilterHashTablePtr ht = NULL;
-
- if (howDetect == 0)
- return 1;
-
- if ( !techdriver->canApplyBasicRules()) {
- virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("IP parameter must be provided since "
- "snooping the IP address does not work "
- "possibly due to missing tools"));
- return 1;
- }
-
- if (VIR_ALLOC(req) < 0) {
- virReportOOMError();
- goto err_no_req;
- }
-
- ht = virNWFilterHashTableCreate(0);
- if (ht == NULL) {
- virReportOOMError();
- goto err_free_req;
- }
-
- if (virNWFilterHashTablePutAll(filterparams, ht))
- goto err_free_ht;
-
- req->filtername = strdup(filtername);
- if (req->filtername == NULL) {
- virReportOOMError();
- goto err_free_ht;
- }
-
- if (virStrcpyStatic(req->ifname, ifname) == NULL) {
- virNWFilterReportError(VIR_ERR_INTERNAL_ERROR,
- _("Destination buffer for ifname ('%s') "
- "not large enough"), ifname);
- goto err_free_ht;
- }
-
- if (linkdev) {
- if (virStrcpyStatic(req->linkdev, linkdev) == NULL) {
- virNWFilterReportError(VIR_ERR_INTERNAL_ERROR,
- _("Destination buffer for linkdev ('%s') "
- "not large enough"), linkdev);
- goto err_free_ht;
- }
- }
-
- req->ifindex = ifindex;
- req->nettype = nettype;
- memcpy(req->macaddr, macaddr, sizeof(req->macaddr));
- req->driver = driver;
- req->filterparams = ht;
- ht = NULL;
- req->howDetect = howDetect;
- req->techdriver = techdriver;
-
- rc = virNWFilterRegisterLearnReq(req);
-
- if (rc)
- goto err_free_req;
-
- if (pthread_create(&req->thread,
- NULL,
- learnIPAddressThread,
- req) != 0)
- goto err_dereg_req;
-
- return 0;
-
-err_dereg_req:
- virNWFilterDeregisterLearnReq(ifindex);
-err_free_ht:
- virNWFilterHashTableFree(ht);
-err_free_req:
- virNWFilterIPAddrLearnReqFree(req);
-err_no_req:
- return 1;
-}
-
-#else
-
-int
-virNWFilterLearnIPAddress(virNWFilterTechDriverPtr techdriver ATTRIBUTE_UNUSED,
- const char *ifname ATTRIBUTE_UNUSED,
- int ifindex ATTRIBUTE_UNUSED,
- const char *linkdev ATTRIBUTE_UNUSED,
- enum virDomainNetType nettype ATTRIBUTE_UNUSED,
- const unsigned char *macaddr ATTRIBUTE_UNUSED,
- const char *filtername ATTRIBUTE_UNUSED,
- virNWFilterHashTablePtr filterparams ATTRIBUTE_UNUSED,
- virNWFilterDriverStatePtr driver ATTRIBUTE_UNUSED,
- enum howDetect howDetect ATTRIBUTE_UNUSED) {
- virNWFilterReportError(VIR_ERR_INTERNAL_ERROR, "%s",
- _("IP parameter must be given since libvirt "
- "was not compiled with IP address learning "
- "support"));
- return 1;
-}
-#endif /* HAVE_LIBPCAP */
-
-
-/**
- * virNWFilterLearnInit
- * Initialization of this layer
- */
-int
-virNWFilterLearnInit(void) {
-
- if (pendingLearnReq)
- return 0;
-
- threadsTerminate = false;
-
- pendingLearnReq = virHashCreate(0, freeLearnReqEntry);
- if (!pendingLearnReq) {
- return 1;
- }
-
- if (virMutexInit(&pendingLearnReqLock)) {
- virNWFilterLearnShutdown();
- return 1;
- }
-
- ipAddressMap = virNWFilterHashTableCreate(0);
- if (!ipAddressMap) {
- virReportOOMError();
- virNWFilterLearnShutdown();
- return 1;
- }
-
- if (virMutexInit(&ipAddressMapLock)) {
- virNWFilterLearnShutdown();
- return 1;
- }
-
- ifaceLockMap = virHashCreate(0, freeIfaceLock);
- if (!ifaceLockMap) {
- virNWFilterLearnShutdown();
- return 1;
- }
-
- if (virMutexInit(&ifaceMapLock)) {
- virNWFilterLearnShutdown();
- return 1;
- }
-
- return 0;
-}
-
-
-void
-virNWFilterLearnThreadsTerminate(bool allowNewThreads) {
- threadsTerminate = true;
-
- while (virHashSize(pendingLearnReq) != 0)
- usleep((PKT_TIMEOUT_MS * 1000) / 3);
-
- if (allowNewThreads)
- threadsTerminate = false;
-}
-
-/**
- * virNWFilterLearnShutdown
- * Shutdown of this layer
- */
-void
-virNWFilterLearnShutdown(void)
-{
- if (!pendingLearnReq)
- return;
-
- virNWFilterLearnThreadsTerminate(false);
-
- virHashFree(pendingLearnReq);
- pendingLearnReq = NULL;
-
- virNWFilterHashTableFree(ipAddressMap);
- ipAddressMap = NULL;
-
- virHashFree(ifaceLockMap);
- ifaceLockMap = NULL;
-}
diff --git a/src/nwfilter/nwfilter_learnipaddr.h b/src/nwfilter/nwfilter_learnipaddr.h
deleted file mode 100644
index e4b9811..0000000
--- a/src/nwfilter/nwfilter_learnipaddr.h
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * nwfilter_learnipaddr.h: support for learning IP address used by a VM
- * on an interface
- *
- * Copyright (C) 2010 IBM Corp.
- * Copyright (C) 2010 Stefan Berger
- *
- * 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, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * Author: Stefan Berger <stefanb(a)us.ibm.com>
- */
-
-#ifndef __NWFILTER_LEARNIPADDR_H
-# define __NWFILTER_LEARNIPADDR_H
-
-enum howDetect {
- DETECT_DHCP = 1,
- DETECT_STATIC = 2,
-};
-
-typedef struct _virNWFilterIPAddrLearnReq virNWFilterIPAddrLearnReq;
-typedef virNWFilterIPAddrLearnReq *virNWFilterIPAddrLearnReqPtr;
-struct _virNWFilterIPAddrLearnReq {
- virNWFilterTechDriverPtr techdriver;
- char ifname[IF_NAMESIZE];
- int ifindex;
- char linkdev[IF_NAMESIZE];
- enum virDomainNetType nettype;
- unsigned char macaddr[VIR_MAC_BUFLEN];
- char *filtername;
- virNWFilterHashTablePtr filterparams;
- virNWFilterDriverStatePtr driver;
- enum howDetect howDetect;
-
- int status;
- pthread_t thread;
- volatile bool terminate;
-};
-
-int virNWFilterLearnIPAddress(virNWFilterTechDriverPtr techdriver,
- const char *ifname,
- int ifindex,
- const char *linkdev,
- enum virDomainNetType nettype,
- const unsigned char *macaddr,
- const char *filtername,
- virNWFilterHashTablePtr filterparams,
- virNWFilterDriverStatePtr driver,
- enum howDetect howDetect);
-
-virNWFilterIPAddrLearnReqPtr virNWFilterLookupLearnReq(int ifindex);
-int virNWFilterTerminateLearnReq(const char *ifname);
-
-void virNWFilterDelIpAddrForIfname(const char *ifname);
-const char *virNWFilterGetIpAddrForIfname(const char *ifname);
-
-int virNWFilterLockIface(const char *ifname) ATTRIBUTE_RETURN_CHECK;
-void virNWFilterUnlockIface(const char *ifname);
-
-int virNWFilterLearnInit(void);
-void virNWFilterLearnShutdown(void);
-void virNWFilterLearnThreadsTerminate(bool allowNewThreads);
-
-#endif /* __NWFILTER_LEARNIPADDR_H */
13 years, 6 months
[libvirt] configure --prefix=$HOME --with-init-script buglet
by Eric Blake
Right now, on a Fedora system, if you configure libvirt to install into
$HOME (because you don't have permissions to install into the default
/usr/local or even into the system /usr), --with-init-script defaults to
auto mode, detects that /etc/redhat-release exists, and tries to install
the init script anyways.
configure.ac needs to be patched to also check that $sysconfdir is /etc
before changing with-init-script=auto over to with-init-script=redhat,
so that the init scripts are only installed during a system install
(where they make sense), and not during a /usr/local or $HOME install.
I don't have time to patch this today, if someone wants to beat me to it.
--
Eric Blake eblake(a)redhat.com +1-801-349-2682
Libvirt virtualization library http://libvirt.org
13 years, 6 months
[libvirt] [PATCH] send 'container=libvirt' in env to container init
by Serge Hallyn
This allows upstart scripts to detect that they are in a container
and modify their behavior accordingly.
In this way, the same ubuntu maverick or natty image with the 'lxcguest'
package installed can be booted on bare metal, in kvm, or as a
libvirt or liblxc container.
Signed-off-by: Chuck Short <zulcss(a)ubuntu.com>
Signed-off-by: Serge Hallyn <serge.hallyn(a)ubuntu.com>
---
src/lxc/lxc_container.c | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
index 9ae93b5..a70aeeb 100644
--- a/src/lxc/lxc_container.c
+++ b/src/lxc/lxc_container.c
@@ -112,6 +112,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomainDefPtr vmDef)
virCommandAddEnvString(cmd, "PATH=/bin:/sbin");
virCommandAddEnvString(cmd, "TERM=linux");
+ virCommandAddEnvString(cmd, "container=libvirt");
virCommandAddEnvPair(cmd, "LIBVIRT_LXC_UUID", uuidstr);
virCommandAddEnvPair(cmd, "LIBVIRT_LXC_NAME", vmDef->name);
--
1.7.4.1
13 years, 6 months
[libvirt] FreeBSD and sys/syslimits.h
by Matthias Bolte
libvirtd.h includes sys/syslimits.h if available. On FreeBSD the
header contains this warning:
In file included from libvirtd.h:42,
from dispatch.h:28,
from dispatch.c:30:
/usr/include/sys/syslimits.h:41:2: warning: #warning "No
user-serviceable parts inside."
Here's the relevant part from that header:
#if !defined(_KERNEL) && !defined(_LIMITS_H_) && !defined(_SYS_PARAM_H_)
#ifndef _SYS_CDEFS_H_
#error this file needs sys/cdefs.h as a prerequisite
#endif
#ifdef __CC_SUPPORTS_WARNING
#warning "No user-serviceable parts inside."
#endif
#endif
This looks like this header is not meant to be included directly, at
least on FreeBSD.
Is this something gnulib or libvirt should care about?
Matthias
13 years, 6 months
[libvirt] [PATCH 0/9 v2] More virCommand conversions and cleanups
by Cole Robinson
The following series converts all users of several older command
wrappers (virRunWithHook, virExecDaemonize, and virExec) to use
virCommand. The remaining functionality is then moved out of
util.c and into command.c
v2:
Committed some patches
Dropped final patch, can be submitted seperately
Addressed Eric's comments
Cole Robinson (9):
storage: iscsi: Convert virExec to virCommand
openvz: Convert virExec usage to virCommand
qemu: Convert virExec usage to virCommand
storage: Covert regex helpers to virCommand
storage_backend: Fix error reporting with regex helper
storage_backend: Convert virRunWithHook usage to virCommand
util: Remove unused virExec wrapper
util: Implement virRun as a wrapper around virCommand
Move virRun, virExec*, virFork to util/command
cfg.mk | 2 +-
src/libvirt_private.syms | 7 +-
src/lxc/veth.c | 2 +-
src/nwfilter/nwfilter_ebiptables_driver.c | 1 +
src/openvz/openvz_conf.c | 37 +-
src/openvz/openvz_driver.c | 55 ++--
src/qemu/qemu_driver.c | 35 +-
src/qemu/qemu_process.c | 2 +-
src/storage/storage_backend.c | 262 ++++--------
src/storage/storage_backend.h | 3 +-
src/storage/storage_backend_fs.c | 7 +-
src/storage/storage_backend_iscsi.c | 39 +--
src/storage/storage_backend_logical.c | 32 +--
src/util/command.c | 568 ++++++++++++++++++++++++
src/util/command.h | 14 +
src/util/ebtables.c | 2 +-
src/util/pci.c | 2 +-
src/util/util.c | 674 +----------------------------
src/util/util.h | 35 --
src/vmware/vmware_driver.c | 1 +
20 files changed, 765 insertions(+), 1015 deletions(-)
--
1.7.4.4
13 years, 6 months
[libvirt] [PATCH] build: tolerate unlimited group size
by Eric Blake
POSIX allows sysconf(_SC_GETPW_R_SIZE_MAX) to return -1 if there
is no fixed limit, and requires ERANGE errors to track real size.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/getpwuid_r.html
Also, on error for get*_r functions, errno is undefined, and the
real error was the return value.
* src/util/util.c (virGetUserEnt, virGetUserID, virGetGroupID)
(virSetUIDGID): Cope with sysconf failure or too small buffer.
Reported by Matthias Bolte.
---
This pretty much copies the example implementation straight from POSIX
for getting a hint for initial size, then iterating until ERANGE no
longer occurs.
src/util/util.c | 74 +++++++++++++++++++++++++++++++++++++-----------------
1 files changed, 51 insertions(+), 23 deletions(-)
diff --git a/src/util/util.c b/src/util/util.c
index f169e54..240f776 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -603,7 +603,7 @@ virExecWithHook(const char *const*argv,
goto fork_error;
}
- openmax = sysconf (_SC_OPEN_MAX);
+ openmax = sysconf(_SC_OPEN_MAX);
for (i = 3; i < openmax; i++)
if (i != infd &&
i != null &&
@@ -2743,11 +2743,11 @@ static char *virGetUserEnt(uid_t uid,
struct passwd *pw = NULL;
long val = sysconf(_SC_GETPW_R_SIZE_MAX);
size_t strbuflen = val;
+ int rc;
- if (val < 0) {
- virReportSystemError(errno, "%s", _("sysconf failed"));
- return NULL;
- }
+ /* sysconf is a hint; if it fails, fall back to a reasonable size */
+ if (val < 0)
+ strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
virReportOOMError();
@@ -2761,8 +2761,15 @@ static char *virGetUserEnt(uid_t uid,
* 0 or ENOENT or ESRCH or EBADF or EPERM or ...
* The given name or uid was not found.
*/
- if (getpwuid_r(uid, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) {
- virReportSystemError(errno,
+ while ((rc = getpwuid_r(uid, &pwbuf, strbuf, strbuflen, &pw)) == ERANGE) {
+ if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0) {
+ virReportOOMError();
+ VIR_FREE(strbuf);
+ return NULL;
+ }
+ }
+ if (rc != 0 || pw == NULL) {
+ virReportSystemError(rc,
_("Failed to find user record for uid '%u'"),
(unsigned int) uid);
VIR_FREE(strbuf);
@@ -2800,11 +2807,11 @@ int virGetUserID(const char *name,
struct passwd *pw = NULL;
long val = sysconf(_SC_GETPW_R_SIZE_MAX);
size_t strbuflen = val;
+ int rc;
- if (val < 0) {
- virReportSystemError(errno, "%s", _("sysconf failed"));
- return -1;
- }
+ /* sysconf is a hint; if it fails, fall back to a reasonable size */
+ if (val < 0)
+ strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
virReportOOMError();
@@ -2818,8 +2825,15 @@ int virGetUserID(const char *name,
* 0 or ENOENT or ESRCH or EBADF or EPERM or ...
* The given name or uid was not found.
*/
- if (getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) {
- virReportSystemError(errno,
+ while ((rc = getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw)) == ERANGE) {
+ if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0) {
+ virReportOOMError();
+ VIR_FREE(strbuf);
+ return -1;
+ }
+ }
+ if (rc != 0 || pw == NULL) {
+ virReportSystemError(rc,
_("Failed to find user record for name '%s'"),
name);
VIR_FREE(strbuf);
@@ -2842,11 +2856,11 @@ int virGetGroupID(const char *name,
struct group *gr = NULL;
long val = sysconf(_SC_GETGR_R_SIZE_MAX);
size_t strbuflen = val;
+ int rc;
- if (val < 0) {
- virReportSystemError(errno, "%s", _("sysconf failed"));
- return -1;
- }
+ /* sysconf is a hint; if it fails, fall back to a reasonable size */
+ if (val < 0)
+ strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) {
virReportOOMError();
@@ -2860,8 +2874,15 @@ int virGetGroupID(const char *name,
* 0 or ENOENT or ESRCH or EBADF or EPERM or ...
* The given name or uid was not found.
*/
- if (getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr) != 0 || gr == NULL) {
- virReportSystemError(errno,
+ while ((rc = getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr)) == ERANGE) {
+ if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0) {
+ virReportOOMError();
+ VIR_FREE(strbuf);
+ return -1;
+ }
+ }
+ if (rc != 0 || gr == NULL) {
+ virReportSystemError(rc,
_("Failed to find group record for name '%s'"),
name);
VIR_FREE(strbuf);
@@ -2897,6 +2918,7 @@ virSetUIDGID(uid_t uid, gid_t gid)
struct passwd pwd, *pwd_result;
char *buf = NULL;
size_t bufsize;
+ int rc;
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1)
@@ -2906,10 +2928,16 @@ virSetUIDGID(uid_t uid, gid_t gid)
virReportOOMError();
return -1;
}
- getpwuid_r(uid, &pwd, buf, bufsize, &pwd_result);
- if (!pwd_result) {
- virReportSystemError(errno,
- _("cannot getpwuid_r(%d)"),
+ while ((rc = getpwuid_r(uid, &pwd, buf, bufsize,
+ &pwd_result)) == ERANGE) {
+ if (VIR_RESIZE_N(buf, bufsize, bufsize, bufsize) < 0) {
+ virReportOOMError();
+ VIR_FREE(buf);
+ return -1;
+ }
+ }
+ if (rc || !pwd_result) {
+ virReportSystemError(rc, _("cannot getpwuid_r(%d)"),
(unsigned int) uid);
VIR_FREE(buf);
return -1;
--
1.7.4.4
13 years, 6 months
[libvirt] 'attach-interface' failure and RHEL6 qemu-kvm
by Neil Wilson
I've run up against a small issue with the 'attach-interface' facility
in later versions of libvirt.
It doesn't appear to work with the vanilla qemu-kvm package in RHEL6
(which reports as version 0.12.1).
For both attach-interface and attach-device I'm getting the same error.
virsh attach-interface srv-vvh2o network vir0001
error: Failed to attach interface
error: internal error unable to add host net: unknown command:
'host_net_add'
virsh # attach-device srv-vvh2o fred.xml
error: Failed to attach device from fred.xml
error: internal error unable to add host net: unknown command:
'host_net_add'
Looking at qemu monitor the 'qemu-kvm' is indeed short of the
'host_net_add' command and it all seems to be down to an entry in
'qemu_capabilities.c' which switches off 'netdev' for qemu versions less
than 13.
Is there a reason that the RHEL6 version - which seems to have
functioning -netdev parameters and 'netdev_add' qemu commands - has been
excluded?
If its a RHEL6 special patched version is there a way to special case
detect it in the code?
Thanks
Neil
13 years, 6 months
[libvirt] [PATCH v2] virsh: Correctly initialize libvirt
by Jiri Denemark
virsh didn't call virInitialize(), which (among other things)
initializes virLastErr thread local variable. As a result of that, virsh
could just segfault in virEventRegisterDefaultImpl() since that is the
first call that touches (resets) virLastErr.
I have no idea what lucky coincidence made this bug visible but I was
able to reproduce it in 100% cases but only in one specific environment
which included building in sandbox.
---
src/libvirt.c | 3 +++
tools/virsh.c | 13 +++++++++----
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/src/libvirt.c b/src/libvirt.c
index 5a5439d..787908e 100644
--- a/src/libvirt.c
+++ b/src/libvirt.c
@@ -369,6 +369,9 @@ static struct gcry_thread_cbs virTLSThreadImpl = {
* in multithreaded applications to avoid potential race when initializing
* the library.
*
+ * Calling virInitialize is mandatory, unless your first API call is one of
+ * virConnectOpen*.
+ *
* Returns 0 in case of success, -1 in case of error
*/
int
diff --git a/tools/virsh.c b/tools/virsh.c
index e35637d..b469e7a 100644
--- a/tools/virsh.c
+++ b/tools/virsh.c
@@ -12983,6 +12983,10 @@ main(int argc, char **argv)
char *defaultConn;
bool ret = true;
+ memset(ctl, 0, sizeof(vshControl));
+ ctl->imode = true; /* default is interactive mode */
+ ctl->log_fd = -1; /* Initialize log file descriptor */
+
if (!setlocale(LC_ALL, "")) {
perror("setlocale");
/* failure to setup locale is not fatal */
@@ -12996,15 +13000,16 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}
+ if (virInitialize() < 0) {
+ vshError(ctl, "%s", _("Failed to initialize libvirt"));
+ return EXIT_FAILURE;
+ }
+
if (!(progname = strrchr(argv[0], '/')))
progname = argv[0];
else
progname++;
- memset(ctl, 0, sizeof(vshControl));
- ctl->imode = true; /* default is interactive mode */
- ctl->log_fd = -1; /* Initialize log file descriptor */
-
if ((defaultConn = getenv("VIRSH_DEFAULT_CONNECT_URI"))) {
ctl->name = vshStrdup(ctl, defaultConn);
}
--
1.7.5.rc3
13 years, 6 months