[libvirt] [PATCH v2 3/5] cgroup macros refactoring
Continue converting to VIR_CGROUP_SUPPORTED --- src/util/vircgroup.c | 378 +++ 1 file changed, 202 insertions(+), 176 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 3640fbd..795897b 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -74,157 +74,6 @@ typedef enum { */ } virCgroupFlags; -bool virCgroupAvailable(void) -{ -bool ret = false; -#ifdef HAVE_GETMNTENT_R -FILE *mounts = NULL; -struct mntent entry; -char buf[CGROUP_MAX_VAL]; - -if (!virFileExists(/proc/cgroups)) -return false; - -if (!(mounts = fopen(/proc/mounts, r))) -return false; - -while (getmntent_r(mounts, entry, buf, sizeof(buf)) != NULL) { -if (STREQ(entry.mnt_type, cgroup)) { -ret = true; -break; -} -} - -VIR_FORCE_FCLOSE(mounts); -#endif -return ret; -} - -#if defined HAVE_MNTENT_H defined HAVE_GETMNTENT_R - -static int virCgroupPartitionEscape(char **path); - -static bool -virCgroupValidateMachineGroup(virCgroupPtr group, - const char *name, - const char *drivername, - const char *partition, - bool stripEmulatorSuffix) -{ -size_t i; -bool valid = false; -char *partname; -char *scopename; - -if (virAsprintf(partname, %s.libvirt-%s, -name, drivername) 0) -goto cleanup; - -if (virCgroupPartitionEscape(partname) 0) -goto cleanup; - -if (!partition) -partition = /machine; - -if (!(scopename = virSystemdMakeScopeName(name, drivername, partition))) -goto cleanup; - -if (virCgroupPartitionEscape(scopename) 0) -goto cleanup; - -for (i = 0; i VIR_CGROUP_CONTROLLER_LAST; i++) { -char *tmp; - -if (i == VIR_CGROUP_CONTROLLER_SYSTEMD) -continue; - -if (!group-controllers[i].placement) -continue; - -tmp = strrchr(group-controllers[i].placement, '/'); -if (!tmp) -goto cleanup; - -if (stripEmulatorSuffix -(i == VIR_CGROUP_CONTROLLER_CPU || - i == VIR_CGROUP_CONTROLLER_CPUACCT || - i == VIR_CGROUP_CONTROLLER_CPUSET)) { -if (STREQ(tmp, /emulator)) -*tmp = '\0'; -tmp = strrchr(group-controllers[i].placement, '/'); -if (!tmp) -goto cleanup; -} - -tmp++; - -if (STRNEQ(tmp, name) -STRNEQ(tmp, partname) -STRNEQ(tmp, scopename)) { -VIR_DEBUG(Name '%s' for controller '%s' does not match '%s', '%s' or '%s', - tmp, virCgroupControllerTypeToString(i), name, partname, scopename); -goto cleanup; -} -} - -valid = true; - - cleanup: -VIR_FREE(partname); -VIR_FREE(scopename); -return valid; -} -#else -static bool -virCgroupValidateMachineGroup(virCgroupPtr group ATTRIBUTE_UNUSED, - const char *name ATTRIBUTE_UNUSED, - const char *drivername ATTRIBUTE_UNUSED, - bool stripEmulatorSuffix ATTRIBUTE_UNUSED) -{ -return true; -} -#endif - -/** - * virCgroupFree: - * - * @group: The group structure to free - */ -void virCgroupFree(virCgroupPtr *group) -{ -size_t i; - -if (*group == NULL) -return; - -for (i = 0; i VIR_CGROUP_CONTROLLER_LAST; i++) { -VIR_FREE((*group)-controllers[i].mountPoint); -VIR_FREE((*group)-controllers[i].linkPoint); -VIR_FREE((*group)-controllers[i].placement); -} - -VIR_FREE((*group)-path); -VIR_FREE(*group); -} - -/** - * virCgroupHasController: query whether a cgroup controller is present - * - * @cgroup: The group structure to be queried, or NULL - * @controller: cgroup subsystem id - * - * Returns true if a cgroup controller is mounted and is associated - * with this cgroup object. - */ -bool virCgroupHasController(virCgroupPtr cgroup, int controller) -{ -if (!cgroup) -return false; -if (controller 0 || controller = VIR_CGROUP_CONTROLLER_LAST) -return false; -return cgroup-controllers[controller].mountPoint != NULL; -} - #if defined HAVE_MNTENT_H defined HAVE_GETMNTENT_R static int virCgroupCopyMounts(virCgroupPtr group, virCgroupPtr parent) @@ -1605,31 +1454,6 @@ int virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED, } #endif -/* - * Returns 0 on success (but @group may be NULL), -1 on fatal error - */ -int virCgroupNewDetectMachine(const char *name, - const char *drivername, - pid_t pid, - const char *partition, - int controllers, - virCgroupPtr
[libvirt] [PATCH v2 2/5] cgroup macros refactoring
- Convert virCgroupGet* to VIR_CGROUP_SUPPORTED - Convert virCgroup(Get|Set)FreezerState to VIR_CGROUP_SUPPORTED - Convert virCgroupRemoveRecursively to VIR_CGROUP_SUPPORTED --- src/util/vircgroup.c | 363 +-- 1 file changed, 204 insertions(+), 159 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 5f656b1..3640fbd 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -57,7 +57,7 @@ #define VIR_FROM_THIS VIR_FROM_CGROUP #if defined(__linux__) defined(HAVE_GETMNTENT_R) \ - defined(_DIRENT_HAVE_D_TYPE) + defined(_DIRENT_HAVE_D_TYPE) defined(_SC_CLK_TCK) # define VIR_CGROUP_SUPPORTED #endif @@ -1000,63 +1000,6 @@ error: } #endif -#if defined _DIRENT_HAVE_D_TYPE -int virCgroupRemoveRecursively(char *grppath) -{ -DIR *grpdir; -struct dirent *ent; -int rc = 0; - -grpdir = opendir(grppath); -if (grpdir == NULL) { -if (errno == ENOENT) -return 0; -rc = -errno; -VIR_ERROR(_(Unable to open %s (%d)), grppath, errno); -return rc; -} - -for (;;) { -char *path; - -errno = 0; -ent = readdir(grpdir); -if (ent == NULL) { -if ((rc = -errno)) -VIR_ERROR(_(Failed to readdir for %s (%d)), grppath, errno); -break; -} - -if (ent-d_name[0] == '.') continue; -if (ent-d_type != DT_DIR) continue; - -if (virAsprintf(path, %s/%s, grppath, ent-d_name) == -1) { -rc = -ENOMEM; -break; -} -rc = virCgroupRemoveRecursively(path); -VIR_FREE(path); -if (rc != 0) -break; -} -closedir(grpdir); - -VIR_DEBUG(Removing cgroup %s, grppath); -if (rmdir(grppath) != 0 errno != ENOENT) { -rc = -errno; -VIR_ERROR(_(Unable to remove %s (%d)), grppath, errno); -} - -return rc; -} -#else -int virCgroupRemoveRecursively(char *grppath ATTRIBUTE_UNUSED) -{ -virReportSystemError(ENXIO, %s, - _(Control groups not supported on this platform)); -return -1; -} -#endif /** * virCgroupRemove: @@ -2585,109 +2528,58 @@ int virCgroupSetCpuCfsQuota(virCgroupPtr group, long long cfs_quota) cpu.cfs_quota_us, cfs_quota); } -/** - * virCgroupGetCpuCfsQuota: - * - * @group: The cgroup to get cpu.cfs_quota_us for - * @cfs_quota: Pointer to the returned cpu bandwidth (in usecs) that this tg - * will be allowed to consume over period - * - * Returns: 0 on success - */ -int virCgroupGetCpuCfsQuota(virCgroupPtr group, long long *cfs_quota) -{ -return virCgroupGetValueI64(group, -VIR_CGROUP_CONTROLLER_CPU, -cpu.cfs_quota_us, cfs_quota); -} - -int virCgroupGetCpuacctUsage(virCgroupPtr group, unsigned long long *usage) -{ -return virCgroupGetValueU64(group, -VIR_CGROUP_CONTROLLER_CPUACCT, -cpuacct.usage, usage); -} - -int virCgroupGetCpuacctPercpuUsage(virCgroupPtr group, char **usage) -{ -return virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT, -cpuacct.usage_percpu, usage); -} - -#ifdef _SC_CLK_TCK -int virCgroupGetCpuacctStat(virCgroupPtr group, unsigned long long *user, -unsigned long long *sys) -{ -char *str; -char *p; -int ret = -1; -static double scale = -1.0; - -if (virCgroupGetValueStr(group, VIR_CGROUP_CONTROLLER_CPUACCT, - cpuacct.stat, str) 0) -return -1; - -if (!(p = STRSKIP(str, user )) || -virStrToLong_ull(p, p, 10, user) 0) { -virReportError(VIR_ERR_INTERNAL_ERROR, - _(Cannot parse user stat '%s'), - p); -goto cleanup; -} -if (!(p = STRSKIP(p, \nsystem )) || -virStrToLong_ull(p, NULL, 10, sys) 0) { -virReportError(VIR_ERR_INTERNAL_ERROR, - _(Cannot parse sys stat '%s'), - p); -goto cleanup; -} -/* times reported are in system ticks (generally 100 Hz), but that - * rate can theoretically vary between machines. Scale things - * into approximate nanoseconds. */ -if (scale 0) { -long ticks_per_sec = sysconf(_SC_CLK_TCK); -if (ticks_per_sec == -1) { -virReportSystemError(errno, %s, - _(Cannot determine system clock HZ)); -goto cleanup; -} -scale = 10.0 / ticks_per_sec; -} -*user *= scale; -*sys *= scale; - -ret = 0; -cleanup: -VIR_FREE(str); -return ret; -} -#else -int virCgroupGetCpuacctStat(virCgroupPtr group ATTRIBUTE_UNUSED, -unsigned long long *user ATTRIBUTE_UNUSED, -unsigned long long *sys
[libvirt] [PATCH v2 1/5] cgroup macros refactoring
- Introduce VIR_CGROUP_SUPPORTED conditional - Convert virCgroupKill* to use it - Convert virCgroupIsolateMount() to use it --- src/util/vircgroup.c | 59 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index cfb4b3f..5f656b1 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -56,6 +56,11 @@ #define VIR_FROM_THIS VIR_FROM_CGROUP +#if defined(__linux__) defined(HAVE_GETMNTENT_R) \ + defined(_DIRENT_HAVE_D_TYPE) +# define VIR_CGROUP_SUPPORTED +#endif + VIR_ENUM_IMPL(virCgroupController, VIR_CGROUP_CONTROLLER_LAST, cpu, cpuacct, cpuset, memory, devices, freezer, blkio, net_cls, perf_event, @@ -2682,7 +2687,7 @@ int virCgroupGetFreezerState(virCgroupPtr group, char **state) } -#if defined HAVE_KILL defined HAVE_MNTENT_H defined HAVE_GETMNTENT_R +#ifdef VIR_CGROUP_SUPPORTED /* * Returns 1 if some PIDs are killed, 0 if none are killed, or -1 on error */ @@ -2866,6 +2871,7 @@ cleanup: return ret; } + int virCgroupKillRecursive(virCgroupPtr group, int signum) { int ret; @@ -2911,31 +2917,7 @@ int virCgroupKillPainfully(virCgroupPtr group) return ret; } -#else /* !(HAVE_KILL, HAVE_MNTENT_H, HAVE_GETMNTENT_R) */ -int virCgroupKill(virCgroupPtr group ATTRIBUTE_UNUSED, - int signum ATTRIBUTE_UNUSED) -{ -virReportSystemError(ENOSYS, %s, - _(Control groups not supported on this platform)); -return -1; -} -int virCgroupKillRecursive(virCgroupPtr group ATTRIBUTE_UNUSED, - int signum ATTRIBUTE_UNUSED) -{ -virReportSystemError(ENOSYS, %s, - _(Control groups not supported on this platform)); -return -1; -} -int virCgroupKillPainfully(virCgroupPtr group ATTRIBUTE_UNUSED) -{ -virReportSystemError(ENOSYS, %s, - _(Control groups not supported on this platform)); -return -1; -} -#endif /* HAVE_KILL, HAVE_MNTENT_H, HAVE_GETMNTENT_R */ - -#ifdef __linux__ static char *virCgroupIdentifyRoot(virCgroupPtr group) { char *ret = NULL; @@ -3048,7 +3030,30 @@ cleanup: VIR_FREE(opts); return ret; } -#else /* __linux__ */ +#else /* VIR_CGROUP_SUPPORTED */ +int virCgroupKill(virCgroupPtr group ATTRIBUTE_UNUSED, + int signum ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + +int virCgroupKillRecursive(virCgroupPtr group ATTRIBUTE_UNUSED, + int signum ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + +int virCgroupKillPainfully(virCgroupPtr group ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + int virCgroupIsolateMount(virCgroupPtr group ATTRIBUTE_UNUSED, const char *oldroot ATTRIBUTE_UNUSED, const char *mountopts ATTRIBUTE_UNUSED) @@ -3057,4 +3062,4 @@ int virCgroupIsolateMount(virCgroupPtr group ATTRIBUTE_UNUSED, _(Control groups not supported on this platform)); return -1; } -#endif /* __linux__ */ +#endif /* VIR_CGROUP_SUPPORTED */ -- 1.8.2.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH v2 5/5] cgroup macros refactoring
Complete the refactoring by adding missing stubs so it compiles on platform without cgroup support. --- src/util/vircgroup.c | 311 ++- 1 file changed, 306 insertions(+), 5 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index e5625f8..2e7cbfa 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -2981,6 +2981,42 @@ int virCgroupPathOfController(virCgroupPtr group ATTRIBUTE_UNUSED, } +int virCgroupRemove(virCgroupPtr group ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupAddTask(virCgroupPtr group ATTRIBUTE_UNUSED, + pid_t pid ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupAddTaskController(virCgroupPtr group ATTRIBUTE_UNUSED, + pid_t pid ATTRIBUTE_UNUSED, + int controller ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupMoveTask(virCgroupPtr src_group ATTRIBUTE_UNUSED, + virCgroupPtr dest_group ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + int virCgroupNewPartition(const char *path ATTRIBUTE_UNUSED, bool create ATTRIBUTE_UNUSED, int controllers ATTRIBUTE_UNUSED, @@ -3032,6 +3068,7 @@ int virCgroupNewEmulator(virCgroupPtr domain ATTRIBUTE_UNUSED, return -1; } + int virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED, int controllers ATTRIBUTE_UNUSED, virCgroupPtr *group ATTRIBUTE_UNUSED) @@ -3042,6 +3079,49 @@ int virCgroupNewDetect(pid_t pid ATTRIBUTE_UNUSED, } +int virCgroupNewMachine(const char *name ATTRIBUTE_UNUSED, +const char *drivername ATTRIBUTE_UNUSED, +bool privileged ATTRIBUTE_UNUSED, +const unsigned char *uuid ATTRIBUTE_UNUSED, +const char *rootdir ATTRIBUTE_UNUSED, +pid_t pidleader ATTRIBUTE_UNUSED, +bool isContainer ATTRIBUTE_UNUSED, +const char *partition ATTRIBUTE_UNUSED, +int controllers ATTRIBUTE_UNUSED, +virCgroupPtr *group ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +bool virCgroupNewIgnoreError(void) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return false; +} + + +int virCgroupSetBlkioWeight(virCgroupPtr group ATTRIBUTE_UNUSED, +unsigned int weight ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupGetBlkioWeight(virCgroupPtr group ATTRIBUTE_UNUSED, +unsigned int *weight ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENXIO, %s, + _(Control groups not supported on this platform)); +return -1; +} + + int virCgroupSetBlkioDeviceWeight(virCgroupPtr group ATTRIBUTE_UNUSED, const char *path ATTRIBUTE_UNUSED, @@ -3053,6 +3133,154 @@ virCgroupSetBlkioDeviceWeight(virCgroupPtr group ATTRIBUTE_UNUSED, } +int virCgroupSetMemory(virCgroupPtr group ATTRIBUTE_UNUSED, + unsigned long long kb ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupGetMemoryUsage(virCgroupPtr group ATTRIBUTE_UNUSED, +unsigned long *kb ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupSetMemoryHardLimit(virCgroupPtr group ATTRIBUTE_UNUSED, +unsigned long long kb ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupGetMemoryHardLimit(virCgroupPtr group ATTRIBUTE_UNUSED, +unsigned long long *kb ATTRIBUTE_UNUSED) +{ +virReportSystemError(ENOSYS, %s, + _(Control groups not supported on this platform)); +return -1; +} + + +int virCgroupSetMemorySoftLimit(virCgroupPtr group ATTRIBUTE_UNUSED, +
[libvirt] [PATCH v2 4/5] cgroup macros refactoring
Complete moving to VIR_CGROUP_SUPPORTED --- src/util/vircgroup.c | 497 --- 1 file changed, 272 insertions(+), 225 deletions(-) diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 795897b..e5625f8 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -74,7 +74,134 @@ typedef enum { */ } virCgroupFlags; -#if defined HAVE_MNTENT_H defined HAVE_GETMNTENT_R + +#ifdef VIR_CGROUP_SUPPORTED +bool virCgroupAvailable(void) +{ +bool ret = false; +FILE *mounts = NULL; +struct mntent entry; +char buf[CGROUP_MAX_VAL]; + +if (!virFileExists(/proc/cgroups)) +return false; + +if (!(mounts = fopen(/proc/mounts, r))) +return false; + +while (getmntent_r(mounts, entry, buf, sizeof(buf)) != NULL) { +if (STREQ(entry.mnt_type, cgroup)) { +ret = true; +break; +} +} + +VIR_FORCE_FCLOSE(mounts); +return ret; +} + + +static int virCgroupPartitionEscape(char **path); + +static bool +virCgroupValidateMachineGroup(virCgroupPtr group, + const char *name, + const char *drivername, + const char *partition, + bool stripEmulatorSuffix) +{ +size_t i; +bool valid = false; +char *partname; +char *scopename; + +if (virAsprintf(partname, %s.libvirt-%s, +name, drivername) 0) +goto cleanup; + +if (virCgroupPartitionEscape(partname) 0) +goto cleanup; + +if (!partition) +partition = /machine; + +if (!(scopename = virSystemdMakeScopeName(name, drivername, partition))) +goto cleanup; + +if (virCgroupPartitionEscape(scopename) 0) +goto cleanup; + +for (i = 0; i VIR_CGROUP_CONTROLLER_LAST; i++) { +char *tmp; + +if (i == VIR_CGROUP_CONTROLLER_SYSTEMD) +continue; + +if (!group-controllers[i].placement) +continue; + +tmp = strrchr(group-controllers[i].placement, '/'); +if (!tmp) +goto cleanup; + +if (stripEmulatorSuffix +(i == VIR_CGROUP_CONTROLLER_CPU || + i == VIR_CGROUP_CONTROLLER_CPUACCT || + i == VIR_CGROUP_CONTROLLER_CPUSET)) { +if (STREQ(tmp, /emulator)) +*tmp = '\0'; +tmp = strrchr(group-controllers[i].placement, '/'); +if (!tmp) +goto cleanup; +} + +tmp++; + +if (STRNEQ(tmp, name) +STRNEQ(tmp, partname) +STRNEQ(tmp, scopename)) { +VIR_DEBUG(Name '%s' for controller '%s' does not match '%s', '%s' or '%s', + tmp, virCgroupControllerTypeToString(i), name, partname, scopename); +goto cleanup; +} +} + +valid = true; + + cleanup: +VIR_FREE(partname); +VIR_FREE(scopename); +return valid; +} + + +/* + * Returns 0 on success (but @group may be NULL), -1 on fatal error + */ +int virCgroupNewDetectMachine(const char *name, + const char *drivername, + pid_t pid, + const char *partition, + int controllers, + virCgroupPtr *group) +{ +if (virCgroupNewDetect(pid, controllers, group) 0) { +if (virCgroupNewIgnoreError()) +return 0; +return -1; +} + +if (!virCgroupValidateMachineGroup(*group, name, drivername, partition, true)) { +VIR_DEBUG(Failed to validate machine name for '%s' driver '%s', + name, drivername); +virCgroupFree(group); +return 0; +} + +return 0; +} + + static int virCgroupCopyMounts(virCgroupPtr group, virCgroupPtr parent) { @@ -346,6 +473,7 @@ cleanup: return ret; } + static int virCgroupDetect(virCgroupPtr group, pid_t pid, int controllers, @@ -453,7 +581,6 @@ static int virCgroupDetect(virCgroupPtr group, return 0; } -#endif int virCgroupPathOfController(virCgroupPtr group, @@ -531,6 +658,7 @@ cleanup: return ret; } + static int virCgroupGetValueStr(virCgroupPtr group, int controller, const char *key, @@ -563,6 +691,7 @@ cleanup: return ret; } + static int virCgroupSetValueU64(virCgroupPtr group, int controller, const char *key, @@ -582,7 +711,6 @@ static int virCgroupSetValueU64(virCgroupPtr group, } - static int virCgroupSetValueI64(virCgroupPtr group, int controller, const char *key, @@ -601,6 +729,7 @@ static int virCgroupSetValueI64(virCgroupPtr
[libvirt] [PATCH] bridge driver: implement networkEnableIpForwarding for BSD
Implement networkEnableIpForwarding() using BSD style sysctl. --- configure.ac| 7 --- src/network/bridge_driver.c | 14 ++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index d3219ce..cc2213d 100644 --- a/configure.ac +++ b/configure.ac @@ -207,7 +207,8 @@ dnl Availability of various common functions (non-fatal if missing), dnl and various less common threadsafe functions AC_CHECK_FUNCS_ONCE([cfmakeraw geteuid getgid getgrnam_r getmntent_r \ getpwuid_r getuid kill mmap newlocale posix_fallocate posix_memalign \ - prlimit regexec sched_getaffinity setgroups setns setrlimit symlink]) + prlimit regexec sched_getaffinity setgroups setns setrlimit symlink \ + sysctlbyname]) dnl Availability of pthread functions (if missing, win32 threading is dnl assumed). Because of $LIB_PTHREAD, we cannot use AC_CHECK_FUNCS_ONCE. @@ -220,8 +221,8 @@ LIBS=$old_libs dnl Availability of various common headers (non-fatal if missing). AC_CHECK_HEADERS([pwd.h paths.h regex.h sys/un.h \ sys/poll.h syslog.h mntent.h net/ethernet.h linux/magic.h \ - sys/un.h sys/syscall.h netinet/tcp.h ifaddrs.h libtasn1.h \ - sys/ucred.h sys/mount.h]) + sys/un.h sys/syscall.h sys/sysctl.h netinet/tcp.h ifaddrs.h \ + libtasn1.h sys/ucred.h sys/mount.h]) dnl Check whether endian provides handy macros. AC_CHECK_DECLS([htole64], [], [], [[#include endian.h]]) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index dd30244..9d070b8 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -41,6 +41,9 @@ #include sys/wait.h #include sys/ioctl.h #include net/if.h +#if HAVE_SYS_SYSCTL_H +# include sys/sysctl.h +#endif #include virerror.h #include datatypes.h @@ -1537,10 +1540,21 @@ static int networkEnableIpForwarding(bool enableIPv4, bool enableIPv6) { int ret = 0; +int enabled = 1; if (enableIPv4) +#ifdef HAVE_SYSCTLBYNAME +ret = sysctlbyname(net.inet.ip.forwarding, NULL, 0, +enabled, sizeof(enabled)); +#else ret = virFileWriteStr(/proc/sys/net/ipv4/ip_forward, 1\n, 0); +#endif if (enableIPv6 ret == 0) +#ifdef HAVE_SYSCTLBYNAME +ret = sysctlbyname(net.inet6.ip6.forwarding, NULL, 0, +enabled, sizeof(enabled)); +#else ret = virFileWriteStr(/proc/sys/net/ipv6/conf/all/forwarding, 1\n, 0); +#endif return ret; } -- 1.8.2.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH v3] BSD: implement virNetDev(Set|Clear)IPv4Address
Provide an implementation of virNetDev(Set|Clear)IPv4Address based on BSD ifconfig tool in addition to 'ip' from Linux iproute2 package. --- configure.ac | 15 +++ src/util/virnetdev.c | 24 2 files changed, 39 insertions(+) diff --git a/configure.ac b/configure.ac index cc2213d..7667430 100644 --- a/configure.ac +++ b/configure.ac @@ -387,6 +387,10 @@ if test $prefix = /usr test $sysconfdir = '${prefix}/etc' ; then sysconfdir='/etc' fi +dnl Specify if we rely on ifconfig instead of iproute2 (e.g. in case +dnl we're working on BSD) +want_ifconfig=no + dnl Make some notes about which OS we're compiling for, as the lxc and qemu dnl drivers require linux headers, and storage_mpath, dtrace, and nwfilter dnl are also linux specific. The network and storage_fs drivers are known @@ -409,6 +413,8 @@ if test $with_linux = no; then fi if test $with_freebsd = yes; then + want_ifconfig=yes + with_firewalld=no fi @@ -2429,6 +2435,15 @@ AC_CHECK_DECLS([BRDGSFD, BRDGADD, BRDGDEL], #include net/if_bridgevar.h ]) +# Check if we need to look for ifconfig +if test $want_ifconfig = yes; then + AC_PATH_PROG([IFCONFIG_PATH], [ifconfig]) + if test -z $IFCONFIG_PATH; then + AC_MSG_ERROR([Failed to find ifconfig.]) + fi + AC_DEFINE_UNQUOTED([IFCONFIG_PATH], $IFCONFIG_PATH, [path to ifconfig binary]) +fi + # Detect when running under the clang static analyzer's scan-build driver # or Coverity-prevent's cov-build. Define STATIC_ANALYSIS accordingly. AC_CACHE_CHECK([whether this build is done by a static analysis tool], diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 30df7a6..8eb4d4c 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -810,12 +810,25 @@ int virNetDevSetIPv4Address(const char *ifname, !(bcaststr = virSocketAddrFormat(broadcast { goto cleanup; } +#ifdef IFCONFIG_PATH +cmd = virCommandNew(IFCONFIG_PATH); +virCommandAddArg(cmd, ifname); +if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET6)) +virCommandAddArg(cmd, inet6); +else +virCommandAddArg(cmd, inet); +virCommandAddArgFormat(cmd, %s/%u, addrstr, prefix); +if (bcaststr) +virCommandAddArgList(cmd, broadcast, bcaststr, NULL); +virCommandAddArg(cmd, alias); +#else cmd = virCommandNew(IP_PATH); virCommandAddArgList(cmd, addr, add, NULL); virCommandAddArgFormat(cmd, %s/%u, addrstr, prefix); if (bcaststr) virCommandAddArgList(cmd, broadcast, bcaststr, NULL); virCommandAddArgList(cmd, dev, ifname, NULL); +#endif if (virCommandRun(cmd, NULL) 0) goto cleanup; @@ -895,10 +908,21 @@ int virNetDevClearIPv4Address(const char *ifname, if (!(addrstr = virSocketAddrFormat(addr))) goto cleanup; +#ifdef IFCONFIG_PATH +cmd = virCommandNew(IFCONFIG_PATH); +virCommandAddArg(cmd, ifname); +if (VIR_SOCKET_ADDR_IS_FAMILY(addr, AF_INET6)) +virCommandAddArg(cmd, inet6); +else +virCommandAddArg(cmd, inet); +virCommandAddArgFormat(cmd, %s/%u, addrstr, prefix); +virCommandAddArg(cmd, -alias); +#else cmd = virCommandNew(IP_PATH); virCommandAddArgList(cmd, addr, del, NULL); virCommandAddArgFormat(cmd, %s/%u, addrstr, prefix); virCommandAddArgList(cmd, dev, ifname, NULL); +#endif if (virCommandRun(cmd, NULL) 0) goto cleanup; -- 1.8.2.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
[libvirt] [PATCH v2] bridge driver: implement networkEnableIpForwarding for BSD
Implement networkEnableIpForwarding() using BSD style sysctl. --- configure.ac| 7 --- src/network/bridge_driver.c | 13 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index d3219ce..cc2213d 100644 --- a/configure.ac +++ b/configure.ac @@ -207,7 +207,8 @@ dnl Availability of various common functions (non-fatal if missing), dnl and various less common threadsafe functions AC_CHECK_FUNCS_ONCE([cfmakeraw geteuid getgid getgrnam_r getmntent_r \ getpwuid_r getuid kill mmap newlocale posix_fallocate posix_memalign \ - prlimit regexec sched_getaffinity setgroups setns setrlimit symlink]) + prlimit regexec sched_getaffinity setgroups setns setrlimit symlink \ + sysctlbyname]) dnl Availability of pthread functions (if missing, win32 threading is dnl assumed). Because of $LIB_PTHREAD, we cannot use AC_CHECK_FUNCS_ONCE. @@ -220,8 +221,8 @@ LIBS=$old_libs dnl Availability of various common headers (non-fatal if missing). AC_CHECK_HEADERS([pwd.h paths.h regex.h sys/un.h \ sys/poll.h syslog.h mntent.h net/ethernet.h linux/magic.h \ - sys/un.h sys/syscall.h netinet/tcp.h ifaddrs.h libtasn1.h \ - sys/ucred.h sys/mount.h]) + sys/un.h sys/syscall.h sys/sysctl.h netinet/tcp.h ifaddrs.h \ + libtasn1.h sys/ucred.h sys/mount.h]) dnl Check whether endian provides handy macros. AC_CHECK_DECLS([htole64], [], [], [[#include endian.h]]) diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index dd30244..80a5bc0 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -41,6 +41,9 @@ #include sys/wait.h #include sys/ioctl.h #include net/if.h +#if HAVE_SYS_SYSCTL_H +# include sys/sysctl.h +#endif #include virerror.h #include datatypes.h @@ -1537,10 +1540,20 @@ static int networkEnableIpForwarding(bool enableIPv4, bool enableIPv6) { int ret = 0; +#ifdef HAVE_SYSCTLBYNAME +int enabled = 1; +if (enableIPv4) +ret = sysctlbyname(net.inet.ip.forwarding, NULL, 0, +enabled, sizeof(enabled)); +if (enableIPv6 ret == 0) +ret = sysctlbyname(net.inet6.ip6.forwarding, NULL, 0, +enabled, sizeof(enabled)); +#else if (enableIPv4) ret = virFileWriteStr(/proc/sys/net/ipv4/ip_forward, 1\n, 0); if (enableIPv6 ret == 0) ret = virFileWriteStr(/proc/sys/net/ipv6/conf/all/forwarding, 1\n, 0); +#endif return ret; } -- 1.8.2.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCHv3] build: more workarounds for if_bridge.h
On Fri, Aug 9, 2013 at 10:59 AM, Eric Blake ebl...@redhat.com wrote: On 08/09/2013 09:38 AM, Daniel P. Berrange wrote: On Wed, Aug 07, 2013 at 05:10:26PM -0600, Eric Blake wrote: This is a second attempt at fixing the problem first attempted in commit 2df8d99; basically undoing the fact that it was reverted in commit 43cee32f, plus fixing two more issues: the code in configure.ac has to EXACTLY match virnetdevbridge.c with regards to declaring in6 types before using if_bridge.h, and the fact that RHEL 5 has even more conflicts: ACK, verified on F19. Thanks; pushed. -- Eric Blake eblake redhat com+1-919-301-3266 Libvirt virtualization library http://libvirt.org I've gone ahead and pushed 43cee32 (revert of 2df8d99) plus this (70024dc) to v1.1.1-maint since v1.1.1-maint did not compile for me on Fedora 19 and on Gentoo with glibc 2.17. -- Doug Goldstein -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [test-API][PATCH] Add network update test case
On 08/09/2013 05:28 PM, hongming zhang wrote: The patch add network update test case to cover network update api. modified: cases/basic_network.conf - The test suite covers add/modify/delete ip-dhcp-host in network. For other test scenario, they can be test via customing the conf file. new file: repos/network/update.py - So far the network update test case only checking when flag is live or current. new file: repos/network/xmls/ip-dhcp-host.xml - Create the ip-dhcp-host sample xml file for adding/deleting ip-dhcp-host new file: repos/network/xmls/modify-ip-dhcp-host.xml - Create the ip-dhcp-host sample xml file for modifying ip-dhcp-host Known bug: - Bug 985782 - Some flag values of method are missing in libvirt-python bindings --- cases/basic_network.conf | 28 ++ repos/network/update.py| 82 repos/network/xmls/ip-dhcp-host.xml|1 + repos/network/xmls/modify-ip-dhcp-host.xml |1 + 4 files changed, 112 insertions(+), 0 deletions(-) create mode 100644 repos/network/update.py create mode 100644 repos/network/xmls/ip-dhcp-host.xml create mode 100644 repos/network/xmls/modify-ip-dhcp-host.xml diff --git a/cases/basic_network.conf b/cases/basic_network.conf index 91d7f21..e9abd57 100644 --- a/cases/basic_network.conf +++ b/cases/basic_network.conf @@ -32,6 +32,34 @@ network:autostart autostart enable +network:update +networkname + $defaultnetname +command +add-first +section +ip-dhcp-host + +network:update +networkname + $defaultnetname +command +modify +section +ip-dhcp-host +xml + xmls/modify-ip-dhcp-host.xml + +network:update +networkname + $defaultnetname +command +delete +section +ip-dhcp-host +xml + xmls/modify-ip-dhcp-host.xml + network:destroy networkname $defaultnetname diff --git a/repos/network/update.py b/repos/network/update.py new file mode 100644 index 000..0024a5e --- /dev/null +++ b/repos/network/update.py @@ -0,0 +1,82 @@ +#!/usr/bin/evn python +#Update a network + +import libvirt +from libvirt import libvirtError +from src import sharedmod + +COMMANDDICT = {none:0, modify:1, delete:2, add-first:4} +SECTIONDICT = {none:0, bridge:1, domain:2, ip:3, ip-dhcp-host:4, \ + ip-dhcp-range:5, forward:6, forward-interface:7,\ + forward-pf:8, portgroup:9, dns-host:10, dns-txt:11,\ + dns-srv:12} +FLAGSDICT = {current:0, live:1, config: 2} + +required_params = ('networkname', ) +optional_params = { + 'command': 'add-first', + 'section': 'ip-dhcp-host', + 'parentIndex': 0, + 'xml': 'xmls/ip-dhcp-host.xml', + 'flag': 'current', + } + +def update(params): +Update a network from xml +global logger +logger = params['logger'] +networkname = params['networkname'] +conn = sharedmod.libvirtobj['conn'] + +command = params['command'] +logger.info(The specified command is %s % command) +section = params['section'] +logger.info(The specified section is %s % section) +parentIndex = int(params.get('parentIndex', 0)) +logger.info(The specified parentIndex is %d % parentIndex) +xmlstr = params.get('xml', 'xmls/ip-dhcp-host.xml').replace('\','\'') +logger.info(The specified updatexml is %s % xmlstr) +flag = params.get('flag', 'current') +logger.info(The specified flag is %s % flag) + +command_val = 0 +section_val = 0 +flag_val = 0 +if COMMANDDICT.has_key(command): +command_val = COMMANDDICT.get(command) +if SECTIONDICT.has_key(section): +section_val = SECTIONDICT.get(section) +if FLAGSDICT.has_key(flag): +flag_val = FLAGSDICT.get(flag) + +try: +network = conn.networkLookupByName(networkname) +logger.info(The original network xml is %s % network.XMLDesc(0)) +network.update(command_val, section_val, parentIndex, xmlstr, \ + flag_val) +updated_netxml = network.XMLDesc(0) +logger.info(The updated network xml is %s % updated_netxml) +#The check only works when flag isn't set as config +if flag_val !=2: +if command_val == 0 or command_val == 2: +if xmlstr not in updated_netxml: +logger.info(Successfully update network) +return 0 +else: +logger.error(Failed to update network) +return 1 + +elif command_val == 1 or command_val == 4: +if xmlstr in updated_netxml: +logger.info(Successfully update network) +return 0 +else: +logger.error(Failed to update network) +return 1 + +except