From f4adeae09dfba242b12098cc303d7b4482a33759 Mon Sep 17 00:00:00 2001 From: John Ferlan Date: Tue, 9 May 2017 16:51:05 -0400 Subject: [PATCH] network: Move macmap mgmt from bridge_driver to virnetworkobj In preparation for having a private virNetworkObj - let's create/move some API's that handle the obj->macmap. The API's will be renamed to have a virNetworkObj prefix to follow conventions and the arguments slightly modified to accept what's necessary to complete their task. Signed-off-by: John Ferlan --- src/conf/virnetworkobj.c | 85 +++++++++++++++++++++++++++++++++++++ src/conf/virnetworkobj.h | 22 ++++++++++ src/libvirt_private.syms | 5 +++ src/network/bridge_driver.c | 80 ++++++---------------------------- 4 files changed, 124 insertions(+), 68 deletions(-) diff --git a/src/conf/virnetworkobj.c b/src/conf/virnetworkobj.c index d2a23e6307..b58af23753 100644 --- a/src/conf/virnetworkobj.c +++ b/src/conf/virnetworkobj.c @@ -107,6 +107,91 @@ virNetworkObjEndAPI(virNetworkObjPtr *net) } +virMacMapPtr +virNetworkObjGetMacMap(virNetworkObjPtr obj) +{ + return obj->macmap; +} + + +void +virNetworkObjSetMacMap(virNetworkObjPtr obj, + virMacMapPtr macmap) +{ + obj->macmap = macmap; +} + + +void +virNetworkObjUnrefMacMap(virNetworkObjPtr obj) +{ + if (!virObjectUnref(obj->macmap)) + obj->macmap = NULL; +} + + +int +virNetworkObjMacMgrAdd(virNetworkObjPtr obj, + const char *dnsmasqStateDir, + const char *domain, + const virMacAddr *mac) +{ + char macStr[VIR_MAC_STRING_BUFLEN]; + char *file = NULL; + int ret = -1; + + if (!obj->macmap) + return 0; + + virMacAddrFormat(mac, macStr); + + if (!(file = virMacMapFileName(dnsmasqStateDir, obj->def->bridge))) + goto cleanup; + + if (virMacMapAdd(obj->macmap, domain, macStr) < 0) + goto cleanup; + + if (virMacMapWriteFile(obj->macmap, file) < 0) + goto cleanup; + + ret = 0; + cleanup: + VIR_FREE(file); + return ret; +} + + +int +virNetworkObjMacMgrDel(virNetworkObjPtr obj, + const char *dnsmasqStateDir, + const char *domain, + const virMacAddr *mac) +{ + char macStr[VIR_MAC_STRING_BUFLEN]; + char *file = NULL; + int ret = -1; + + if (!obj->macmap) + return 0; + + virMacAddrFormat(mac, macStr); + + if (!(file = virMacMapFileName(dnsmasqStateDir, obj->def->bridge))) + goto cleanup; + + if (virMacMapRemove(obj->macmap, domain, macStr) < 0) + goto cleanup; + + if (virMacMapWriteFile(obj->macmap, file) < 0) + goto cleanup; + + ret = 0; + cleanup: + VIR_FREE(file); + return ret; +} + + virNetworkObjListPtr virNetworkObjListNew(void) { diff --git a/src/conf/virnetworkobj.h b/src/conf/virnetworkobj.h index cb1fd13a36..3e42835ecf 100644 --- a/src/conf/virnetworkobj.h +++ b/src/conf/virnetworkobj.h @@ -50,6 +50,28 @@ struct _virNetworkObj { virNetworkObjPtr virNetworkObjNew(void); +virMacMapPtr +virNetworkObjGetMacMap(virNetworkObjPtr obj); + +void +virNetworkObjSetMacMap(virNetworkObjPtr obj, + virMacMapPtr macmap); + +void +virNetworkObjUnrefMacMap(virNetworkObjPtr obj); + +int +virNetworkObjMacMgrAdd(virNetworkObjPtr obj, + const char *dnsmasqStateDir, + const char *domain, + const virMacAddr *mac); + +int +virNetworkObjMacMgrDel(virNetworkObjPtr obj, + const char *dnsmasqStateDir, + const char *domain, + const virMacAddr *mac); + void virNetworkObjEndAPI(virNetworkObjPtr *net); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index d1b0ce5da2..a044af6ec2 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -942,6 +942,7 @@ virNetworkObjDeleteConfig; virNetworkObjEndAPI; virNetworkObjFindByName; virNetworkObjFindByUUID; +virNetworkObjGetMacMap; virNetworkObjGetPersistentDef; virNetworkObjListExport; virNetworkObjListForEach; @@ -951,12 +952,16 @@ virNetworkObjListNumOfNetworks; virNetworkObjListPrune; virNetworkObjLoadAllConfigs; virNetworkObjLoadAllState; +virNetworkObjMacMgrAdd; +virNetworkObjMacMgrDel; virNetworkObjNew; virNetworkObjRemoveInactive; virNetworkObjReplacePersistentDef; virNetworkObjSaveStatus; virNetworkObjSetDefTransient; +virNetworkObjSetMacMap; virNetworkObjTaint; +virNetworkObjUnrefMacMap; virNetworkObjUnsetDefTransient; virNetworkObjUpdate; virNetworkObjUpdateAssignDef; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index de2e83c9f8..a73074250e 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -400,68 +400,6 @@ networkRemoveInactive(virNetworkDriverStatePtr driver, } -static int -networkMacMgrAdd(virNetworkDriverStatePtr driver, - virNetworkObjPtr obj, - const char *domain, - const virMacAddr *mac) -{ - char macStr[VIR_MAC_STRING_BUFLEN]; - char *file = NULL; - int ret = -1; - - if (!obj->macmap) - return 0; - - virMacAddrFormat(mac, macStr); - - if (!(file = virMacMapFileName(driver->dnsmasqStateDir, obj->def->bridge))) - goto cleanup; - - if (virMacMapAdd(obj->macmap, domain, macStr) < 0) - goto cleanup; - - if (virMacMapWriteFile(obj->macmap, file) < 0) - goto cleanup; - - ret = 0; - cleanup: - VIR_FREE(file); - return ret; -} - - -static int -networkMacMgrDel(virNetworkDriverStatePtr driver, - virNetworkObjPtr obj, - const char *domain, - const virMacAddr *mac) -{ - char macStr[VIR_MAC_STRING_BUFLEN]; - char *file = NULL; - int ret = -1; - - if (!obj->macmap) - return 0; - - virMacAddrFormat(mac, macStr); - - if (!(file = virMacMapFileName(driver->dnsmasqStateDir, obj->def->bridge))) - goto cleanup; - - if (virMacMapRemove(obj->macmap, domain, macStr) < 0) - goto cleanup; - - if (virMacMapWriteFile(obj->macmap, file) < 0) - goto cleanup; - - ret = 0; - cleanup: - VIR_FREE(file); - return ret; -} - - static char * networkBridgeDummyNicName(const char *brname) { @@ -493,6 +431,7 @@ networkUpdateState(virNetworkObjPtr obj, { virNetworkDriverStatePtr driver = opaque; dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver); + virMacMapPtr macmap; char *macMapFile = NULL; int ret = -1; @@ -515,9 +454,11 @@ networkUpdateState(virNetworkObjPtr obj, obj->def->bridge))) goto cleanup; - if (!(obj->macmap = virMacMapNew(macMapFile))) + if (!(macmap = virMacMapNew(macMapFile))) goto cleanup; + virNetworkObjSetMacMap(obj, macmap); + break; case VIR_NETWORK_FORWARD_BRIDGE: @@ -2321,6 +2262,7 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver, virNetworkIPDefPtr ipdef; virNetDevIPRoutePtr routedef; char *macTapIfName = NULL; + virMacMapPtr macmap; char *macMapFile = NULL; int tapfd = -1; @@ -2371,9 +2313,11 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver, if (!(macMapFile = virMacMapFileName(driver->dnsmasqStateDir, obj->def->bridge)) || - !(obj->macmap = virMacMapNew(macMapFile))) + !(macmap = virMacMapNew(macMapFile))) goto err1; + virNetworkObjSetMacMap(obj, macmap); + /* Set bridge options */ /* delay is configured in seconds, but virNetDevBridgeSetSTPDelay @@ -2536,8 +2480,7 @@ networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver, if (obj->def->bandwidth) virNetDevBandwidthClear(obj->def->bridge); - if (!virObjectUnref(obj->macmap)) - obj->macmap = NULL; + virNetworkObjUnrefMacMap(obj); if (obj->radvdPid > 0) { char *radvdpidbase; @@ -4653,7 +4596,8 @@ networkAllocateActualDevice(virDomainDefPtr dom, } } - if (networkMacMgrAdd(driver, obj, dom->name, &iface->mac) < 0) + if (virNetworkObjMacMgrAdd(obj, driver->dnsmasqStateDir, + dom->name, &iface->mac) < 0) goto error; if (virNetDevVPortProfileCheckComplete(virtport, true) < 0) @@ -5075,7 +5019,7 @@ networkReleaseActualDevice(virDomainDefPtr dom, } success: - networkMacMgrDel(driver, obj, dom->name, &iface->mac); + virNetworkObjMacMgrDel(obj, driver->dnsmasqStateDir, dom->name, &iface->mac); if (iface->data.network.actual) { netdef->connections--;