Skip to content

Commit

Permalink
Short canonical interface names in "show vlan".
Browse files Browse the repository at this point in the history
Moved canonical_if_name to if_generic.c and implemented short_if_name with the
same functionality for now. The main reason for separating behavior for long
canonical names and for short canonical names is that there may be different
cases when we add more interface types.

Also, ports are listed alphabetically now.
  • Loading branch information
acrish authored and Lisa Virtual Machine committed Feb 21, 2013
1 parent 277eadc commit 4069be7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 27 deletions.
24 changes: 0 additions & 24 deletions userspace/cli/cfgbuild/cfgbuild.c
Expand Up @@ -49,30 +49,6 @@ static __inline__ void list_vlans(FILE *out, unsigned char *bmp)
fputc('\n', out);
}

static char *canonical_if_name(struct net_switch_dev *nsdev)
{
char *ret = NULL;
int n, status = -1;

if (nsdev == NULL)
return NULL;

switch (nsdev->type) {
case SW_IF_SWITCHED:
case SW_IF_ROUTED:
if ((n = if_parse_ethernet(nsdev->name)) >= 0)
status = asprintf(&ret, "Ethernet %d", n);
else
status = asprintf(&ret, "netdev %s", nsdev->name);
break;
case SW_IF_VIF:
status = asprintf(&ret, "vlan %d", nsdev->vlan);
break;
}

return status == -1 ? NULL : ret;
}

#define nbit2mask(nbit) (htonl(nbit ? (~((uint32_t)0)) ^ ((((uint32_t)1) << (32 - nbit)) - 1) : 0))
int build_config_interface(struct cli_context *ctx, FILE *out, struct net_switch_dev *nsdev, int if_cmd)
{
Expand Down
20 changes: 17 additions & 3 deletions userspace/cli/command/exec.c
Expand Up @@ -49,6 +49,13 @@ static inline int comma_buffer_reset(struct comma_buffer *buf, const char *str)
buf->offset = str_len;
return 0;
}
static inline int cmpstr(void const *str1, void const *str2)
{
char const *s1 = (char const *)str1;
char const *s2 = (char const *)str2;

return strcmp(s1, s2);
}

int swcli_output_modifiers_run(struct cli_context *ctx, int argc, char **argv,
struct menu_node **nodev)
Expand Down Expand Up @@ -889,17 +896,24 @@ int cmd_show_vlan(struct cli_context *ctx, int argc, char **argv, struct menu_no
assert(vlif_no >= 0);

vlif_no /= sizeof(int);
/* Sort interface names and print them comma separated. */
char names[vlif_no][MAXSIZ_IFNAMSH];
for (j = 0; j < vlif_no; j++) {
int ifindex = ((int *)vlif_swcfgr.buf.addr)[j];
int tmp;

nsdev = if_map_lookup_ifindex(&if_map, ifindex, sock_fd);
assert(nsdev);

if (comma_buffer_append(&buf, nsdev->name)) {
strcpy(names[j], short_if_name(nsdev));
}
qsort(names, vlif_no, MAXSIZ_IFNAMSH * sizeof(char), cmpstr);
for (j = 0; j < vlif_no; j++) {
if (comma_buffer_append(&buf, names[j])) {
int tmp;

print_buf;
firstline = 0;
tmp = comma_buffer_reset(&buf, nsdev->name);
tmp = comma_buffer_reset(&buf, names[j]);
assert(!tmp);
}
}
Expand Down
7 changes: 7 additions & 0 deletions userspace/include/if_generic.h
Expand Up @@ -18,6 +18,9 @@
#include "netlink.h"
#include "list.h"

/* Maximum size of interface name in short format: strlen("net ")+IFNAMSIZ */
#define MAXSIZ_IFNAMSH (4 + IFNAMSIZ)

/* Build a linux netdevice name with a generic structure of "type"
* immediately followed by "numeric identifier" (e.g. "eth1").
*
Expand Down Expand Up @@ -54,6 +57,10 @@ int if_parse_generic(const char *name, const char *type);

int if_get_index(const char *name, int sock_fd);

char *canonical_if_name(struct net_switch_dev *nsdev);

char *short_if_name(struct net_switch_dev *nsdev);

char *if_get_name(int if_index, int sock_fd, char *name);

struct if_addr {
Expand Down
52 changes: 52 additions & 0 deletions userspace/lib/if_generic.c
@@ -1,3 +1,5 @@
// define _GNU_SOURCE supresses the warning for asprintf
#define _GNU_SOURCE
#include <sys/ioctl.h>

#include "if_generic.h"
Expand Down Expand Up @@ -40,6 +42,56 @@ int if_get_index(const char *name, int sock_fd)
return ret;
}

char *canonical_if_name(struct net_switch_dev *nsdev)
{
char *ret = NULL;
int n, status = -1;

if (nsdev == NULL)
return NULL;

switch (nsdev->type) {
case SW_IF_SWITCHED:
case SW_IF_ROUTED:
if ((n = if_parse_ethernet(nsdev->name)) >= 0)
status = asprintf(&ret, "Ethernet %d", n);
else
status = asprintf(&ret, "netdev %s", nsdev->name);
break;
case SW_IF_VIF:
status = asprintf(&ret, "vlan %d", nsdev->vlan);
break;
}

return status == -1 ? NULL : ret;
}

char *short_if_name(struct net_switch_dev *nsdev)
{
char *ret = NULL;
int n, status = -1;

if (nsdev == NULL)
return NULL;

switch (nsdev->type) {
case SW_IF_SWITCHED:
case SW_IF_ROUTED:
if ((n = if_parse_ethernet(nsdev->name)) >= 0)
status = asprintf(&ret, "Et%d", n);
else
status = asprintf(&ret, "net%s", nsdev->name);
break;
case SW_IF_VIF:
status = asprintf(&ret, "vlan %d", nsdev->vlan);
break;
}

return status == -1 ? NULL : ret;
}



char *if_get_name(int if_index, int sock_fd, char *name)
{
struct ifreq ifr;
Expand Down

0 comments on commit 4069be7

Please sign in to comment.