network: improve network logging

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
Christian Brauner 2017-06-18 11:31:39 +02:00
parent c302b47632
commit 9b0df30f9d
No known key found for this signature in database
GPG Key ID: 7B3C391EFEA93624
3 changed files with 124 additions and 58 deletions

View File

@ -685,8 +685,6 @@ static int set_config_network(const char *key, const char *value,
return clr_config_network(key, lxc_conf, data); return clr_config_network(key, lxc_conf, data);
} }
static int macvlan_mode(int *valuep, const char *value);
static int set_config_network_type(const char *key, const char *value, static int set_config_network_type(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data) struct lxc_conf *lxc_conf, void *data)
{ {
@ -728,7 +726,8 @@ static int set_config_network_type(const char *key, const char *value,
netdev->type = LXC_NET_VETH; netdev->type = LXC_NET_VETH;
} else if (!strcmp(value, "macvlan")) { } else if (!strcmp(value, "macvlan")) {
netdev->type = LXC_NET_MACVLAN; netdev->type = LXC_NET_MACVLAN;
macvlan_mode(&netdev->priv.macvlan_attr.mode, "private"); lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode,
"private");
} else if (!strcmp(value, "vlan")) { } else if (!strcmp(value, "vlan")) {
netdev->type = LXC_NET_VLAN; netdev->type = LXC_NET_VLAN;
} else if (!strcmp(value, "phys")) { } else if (!strcmp(value, "phys")) {
@ -854,47 +853,6 @@ static int network_ifname(char **valuep, const char *value)
return set_config_string_item_max(valuep, value, IFNAMSIZ); return set_config_string_item_max(valuep, value, IFNAMSIZ);
} }
#ifndef MACVLAN_MODE_PRIVATE
#define MACVLAN_MODE_PRIVATE 1
#endif
#ifndef MACVLAN_MODE_VEPA
#define MACVLAN_MODE_VEPA 2
#endif
#ifndef MACVLAN_MODE_BRIDGE
#define MACVLAN_MODE_BRIDGE 4
#endif
#ifndef MACVLAN_MODE_PASSTHRU
#define MACVLAN_MODE_PASSTHRU 8
#endif
static int macvlan_mode(int *valuep, const char *value)
{
struct mc_mode {
char *name;
int mode;
} m[] = {
{ "private", MACVLAN_MODE_PRIVATE },
{ "vepa", MACVLAN_MODE_VEPA },
{ "bridge", MACVLAN_MODE_BRIDGE },
{ "passthru", MACVLAN_MODE_PASSTHRU },
};
size_t i;
for (i = 0; i < sizeof(m) / sizeof(m[0]); i++) {
if (strcmp(m[i].name, value))
continue;
*valuep = m[i].mode;
return 0;
}
return -1;
}
static int rand_complete_hwaddr(char *hwaddr) static int rand_complete_hwaddr(char *hwaddr)
{ {
const char hex[] = "0123456789abcdef"; const char hex[] = "0123456789abcdef";
@ -1099,7 +1057,7 @@ static int set_config_network_macvlan_mode(const char *key, const char *value,
if (!netdev) if (!netdev)
return -1; return -1;
return macvlan_mode(&netdev->priv.macvlan_attr.mode, value); return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
} }
static int set_config_network_hwaddr(const char *key, const char *value, static int set_config_network_hwaddr(const char *key, const char *value,

View File

@ -22,6 +22,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include "conf.h" #include "conf.h"
#include "confile.h" #include "confile.h"
@ -242,18 +243,35 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
} }
lxc_list_for_each(it, &conf->network) { lxc_list_for_each(it, &conf->network) {
struct lxc_list *cur, *next;
struct lxc_inetdev *inet4dev;
struct lxc_inet6dev *inet6dev;
char bufinet4[INET_ADDRSTRLEN], bufinet6[INET6_ADDRSTRLEN];
netdev = it->elem; netdev = it->elem;
TRACE("index: %zd", netdev->idx); TRACE("index: %zd", netdev->idx);
switch (netdev->type) { switch (netdev->type) {
case LXC_NET_VETH: case LXC_NET_VETH:
TRACE("type: veth"); TRACE("type: veth");
if (netdev->priv.veth_attr.pair)
TRACE("veth pair: %s",
netdev->priv.veth_attr.pair);
break; break;
case LXC_NET_MACVLAN: case LXC_NET_MACVLAN:
TRACE("type: macvlan"); TRACE("type: macvlan");
if (netdev->priv.macvlan_attr.mode > 0) {
char *macvlan_mode;
macvlan_mode = lxc_macvlan_flag_to_mode(
netdev->priv.macvlan_attr.mode);
TRACE("macvlan mode: %s",
macvlan_mode ? macvlan_mode
: "(invalid mode)");
}
break; break;
case LXC_NET_VLAN: case LXC_NET_VLAN:
TRACE("type: vlan"); TRACE("type: vlan");
TRACE("vlan id: %d", netdev->priv.vlan_attr.vid);
break; break;
case LXC_NET_PHYS: case LXC_NET_PHYS:
TRACE("type: phys"); TRACE("type: phys");
@ -269,7 +287,9 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
return; return;
} }
TRACE("flags: %s", netdev->flags == IFF_UP ? "up" : "none"); if (netdev->type != LXC_NET_EMPTY) {
TRACE("flags: %s",
netdev->flags == IFF_UP ? "up" : "none");
if (netdev->link) if (netdev->link)
TRACE("link: %s", netdev->link); TRACE("link: %s", netdev->link);
if (netdev->name) if (netdev->name)
@ -282,6 +302,37 @@ void lxc_log_configured_netdevs(const struct lxc_conf *conf)
TRACE("upscript: %s", netdev->upscript); TRACE("upscript: %s", netdev->upscript);
if (netdev->downscript) if (netdev->downscript)
TRACE("downscript: %s", netdev->downscript); TRACE("downscript: %s", netdev->downscript);
TRACE("ipv4 gateway auto: %s",
netdev->ipv4_gateway_auto ? "true" : "false");
if (netdev->ipv4_gateway) {
inet_ntop(AF_INET, netdev->ipv4_gateway,
bufinet4, sizeof(bufinet4));
TRACE("ipv4 gateway: %s", bufinet4);
}
lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
inet4dev = cur->elem;
inet_ntop(AF_INET, &inet4dev->addr, bufinet4,
sizeof(bufinet4));
TRACE("ipv4 addr: %s", bufinet4);
}
TRACE("ipv6 gateway auto: %s",
netdev->ipv6_gateway_auto ? "true" : "false");
if (netdev->ipv6_gateway) {
inet_ntop(AF_INET6, netdev->ipv6_gateway,
bufinet6, sizeof(bufinet6));
TRACE("ipv6 gateway: %s", bufinet6);
}
lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
inet6dev = cur->elem;
inet_ntop(AF_INET6, &inet6dev->addr, bufinet6,
sizeof(bufinet6));
TRACE("ipv6 addr: %s", bufinet6);
}
}
} }
} }
@ -354,3 +405,42 @@ void lxc_free_networks(struct lxc_list *networks)
/* prevent segfaults */ /* prevent segfaults */
lxc_list_init(networks); lxc_list_init(networks);
} }
static struct macvlan_mode {
char *name;
int mode;
} macvlan_mode[] = {
{ "private", MACVLAN_MODE_PRIVATE },
{ "vepa", MACVLAN_MODE_VEPA },
{ "bridge", MACVLAN_MODE_BRIDGE },
{ "passthru", MACVLAN_MODE_PASSTHRU },
};
int lxc_macvlan_mode_to_flag(int *mode, const char *value)
{
size_t i;
for (i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) {
if (strcmp(macvlan_mode[i].name, value))
continue;
*mode = macvlan_mode[i].mode;
return 0;
}
return -1;
}
char *lxc_macvlan_flag_to_mode(int mode)
{
size_t i;
for (i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) {
if (macvlan_mode[i].mode == mode)
continue;
return macvlan_mode[i].name;
}
return NULL;
}

View File

@ -24,6 +24,22 @@
#include "conf.h" #include "conf.h"
#ifndef MACVLAN_MODE_PRIVATE
#define MACVLAN_MODE_PRIVATE 1
#endif
#ifndef MACVLAN_MODE_VEPA
#define MACVLAN_MODE_VEPA 2
#endif
#ifndef MACVLAN_MODE_BRIDGE
#define MACVLAN_MODE_BRIDGE 4
#endif
#ifndef MACVLAN_MODE_PASSTHRU
#define MACVLAN_MODE_PASSTHRU 8
#endif
extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
unsigned long *hostid, unsigned long *range); unsigned long *hostid, unsigned long *range);
@ -35,5 +51,7 @@ lxc_get_netdev_by_idx(struct lxc_conf *conf, unsigned int idx, bool allocate);
extern void lxc_log_configured_netdevs(const struct lxc_conf *conf); extern void lxc_log_configured_netdevs(const struct lxc_conf *conf);
extern bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx); extern bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx);
extern void lxc_free_networks(struct lxc_list *networks); extern void lxc_free_networks(struct lxc_list *networks);
extern int lxc_macvlan_mode_to_flag(int *mode, const char *value);
extern char *lxc_macvlan_flag_to_mode(int mode);
#endif /* __LXC_CONFILE_UTILS_H */ #endif /* __LXC_CONFILE_UTILS_H */