Skip to content

Commit

Permalink
bhyve: change vale port from jname to port id
Browse files Browse the repository at this point in the history
vale has a limited port name length. This is a problem with long $jname.
add next-vale-port tools to determine next free VALE port
  • Loading branch information
olevole committed May 29, 2020
1 parent a92c257 commit dcdfa38
Show file tree
Hide file tree
Showing 15 changed files with 284 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -20,6 +20,7 @@ misc/resolv
misc/sqlcli
misc/ipv6range
misc/sipcalc
misc/next-vale-port
sbin/netmask
tools/bridge
tools/imghelper
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Expand Up @@ -46,6 +46,7 @@ distclean:
${RM} -f misc/daemon
${RM} -f misc/resolv
${RM} -f misc/ipv6range
${RM} -f misc/next-vale-port
${RM} -f tools/imghelper
${RM} -f tools/xo
${RM} -f tools/vale-ctl
Expand Down Expand Up @@ -100,6 +101,7 @@ cbsd: pkg-config-check
${CC} misc/src/daemon.c -lutil -o misc/daemon && ${STRIP} misc/daemon
${CC} misc/src/resolv.c -o misc/resolv && ${STRIP} misc/resolv
${CC} misc/src/ipv6range.c -o misc/ipv6range && ${STRIP} misc/ipv6range
${CC} misc/src/next-vale-port.c -o misc/next-vale-port && ${STRIP} misc/next-vale-port
${CC} tools/src/imghelper.c -o tools/imghelper && ${STRIP} tools/imghelper
${CC} tools/src/bridge.c -o tools/bridge && ${STRIP} tools/bridge
${CC} tools/src/vale-ctl.c -o tools/vale-ctl && ${STRIP} tools/vale-ctl
Expand Down
8 changes: 4 additions & 4 deletions bhyve.subr
Expand Up @@ -767,7 +767,7 @@ compile_nic_args()
local _pcislot_args= _pref
local IFS OIFS

. ${distdir}/vnet.subr # get_vm_uplink_interface
. ${distdir}/vnet.subr # for get_vm_uplink_interface

local errmsg=

Expand Down Expand Up @@ -800,10 +800,10 @@ compile_nic_args()
case ${nic_parent} in

cbsdvale*)
get_vm_uplink_interface ${nic_parent}
get_vm_uplink_interface -p ${nic_parent} -u
;;
vale*)
get_vm_uplink_interface ${nic_parent}
get_vm_uplink_interface -p ${nic_parent} -u
;;
*)
# VPC support
Expand Down Expand Up @@ -836,7 +836,7 @@ compile_nic_args()
if [ "${_is_bridge}" != "bridge" ]; then
# this is not bridge, detect uplink iface
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: ${nic_parent} is not bridge
get_vm_uplink_interface ${nic_parent}
get_vm_uplink_interface -p ${nic_parent}
cbsdlogger NOTICE ${CBSD_APP}: compile_nic_args for ${jname}: uplink interface selected: ${interface}

if [ "${interface}" != "disable" ]; then
Expand Down
18 changes: 9 additions & 9 deletions etc/defaults/vm-openbsd-x86-6.conf
Expand Up @@ -2,19 +2,19 @@
vm_profile="x86-6"
vm_os_type="openbsd"
# this is one-string additional info strings in dialogue menu
long_description="OpenBSD 6.6-RELEASE x86-64"
long_description="OpenBSD 6.7-RELEASE x86-64"

# custom settings:
fetch=1

# Official resources to fetch ISO's
iso_site="\
https://fastly.cdn.openbsd.org/pub/OpenBSD/6.6/amd64/ \
https://ftp.hostserver.de/pub/OpenBSD/6.6/amd64/ \
http://ftp.spline.de/pub/OpenBSD/6.6/amd64/ \
http://ftp.hostserver.de/pub/OpenBSD/6.6/amd64/ \
https://fastly.cdn.openbsd.org/pub/OpenBSD/6.7/amd64/ \
https://ftp.hostserver.de/pub/OpenBSD/6.7/amd64/ \
http://ftp.spline.de/pub/OpenBSD/6.7/amd64/ \
http://ftp.hostserver.de/pub/OpenBSD/6.7/amd64/ \
ftp://ftp.openbsd.org/pub/OpenBSD/snapshots/amd64/ \
http://mirror.internode.on.net/pub/OpenBSD/6.6/amd64/ \
http://mirror.internode.on.net/pub/OpenBSD/6.7/amd64/ \
"

# Official CBSD project mirrors
Expand All @@ -25,7 +25,7 @@ http://mirror.internode.on.net/pub/OpenBSD/6.6/amd64/ \
# clonos.ca.ircdriven.net clonos.us.ircdriven.net: onecoldworld at gmail dot com
cbsd_iso_mirrors="http://cbsd.lifec0re.net/iso/ http://electro.bsdstore.ru/iso/ https://mirror.bsdstore.ru/iso/ https://clonos.ca.ircdriven.net/iso/ https://clonos.us.ircdriven.net/iso/ https://electrode.bsdstore.ru/iso/"

iso_img="install66.fs"
iso_img="install67.fs"

# register_iso as:
register_iso_name="cbsd-iso-${iso_img}"
Expand Down Expand Up @@ -55,8 +55,8 @@ clonos_active=1

# VirtualBox Area
virtualbox_ostype="OpenBSD_64"
sha256sum="0e40d80abb7b46b2309063600997c91c83e576ae07cf3007385095b10948a410"
iso_img_dist_size="472317952"
sha256sum="81cb267e646eea742833cae2bca59de954ece1d170cc97a57c931645fa40acdb"
iso_img_dist_size="476545024"

# enable birtio RNG interface?
virtio_rnd="0"
3 changes: 3 additions & 0 deletions jailctl/bcleanup
Expand Up @@ -37,6 +37,9 @@ while [ ${nic_num} -ne -1 ]; do
nic_num=$(( nic_num -1 ))
done

# delete orphaned vale_ports if exists
cbsdsqlrw local "DELETE FROM vale_ports WHERE jname=\"${jname}\""

# cleanup ipfw counter
fwcounters jname=${jname} mode=remove
exit 0
8 changes: 4 additions & 4 deletions jcreate.subr
Expand Up @@ -344,7 +344,7 @@ export_bhyve_data_for_external_hook()
{
local nic_id _i T

. ${distdir}/vnet.subr # get_vm_uplink_interface
. ${distdir}/vnet.subr # for get_vm_uplink_interface

# export variables for external hooks
export jname=${jname}
Expand All @@ -368,7 +368,7 @@ export_bhyve_data_for_external_hook()

eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite "SELECT nic_hwaddr,nic_parent FROM bhyvenic" | while read nic_hwaddr nic_parent; do
echo "export nic_hwaddr${nic_id}=\"${nic_hwaddr}\""
get_vm_uplink_interface ${nic_parent}
get_vm_uplink_interface -p ${nic_parent}
echo "export nic_parent${nic_id}=\"${interface}\""
nic_id=$(( nic_id + 1 ))
done )
Expand All @@ -380,7 +380,7 @@ export_jail_data_for_external_hook()
{
local nic_id _i T

. ${distdir}/vnet.subr # get_vm_uplink_interface
. ${distdir}/vnet.subr # for get_vm_uplink_interface

# export variables for external hooks
# todo: first_ipv4_addr + first_ipv6_addr
Expand All @@ -401,7 +401,7 @@ export_jail_data_for_external_hook()

eval $( cbsdsqlro ${jailsysdir}/${jname}/local.sqlite "SELECT nic_hwaddr,nic_parent FROM jailnic" | while read nic_hwaddr nic_parent; do
echo "export nic_hwaddr${nic_id}=\"${nic_hwaddr}\""
get_vm_uplink_interface ${nic_parent}
get_vm_uplink_interface -p ${nic_parent}
echo "export nic_parent${nic_id}=\"${interface}\""
nic_id=$(( nic_id + 1 ))
done )
Expand Down
192 changes: 192 additions & 0 deletions misc/src/next-vale-port.c
@@ -0,0 +1,192 @@
// CBSD Project 2013-2020
// CBSD Team <cbsd+subscribe@lists.tilda.center>
// 0.1
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/netmap_user.h>
#include <net/netmap.h>
#include <errno.h>

#define is_digit(c) ((unsigned int)((c) - '0') <= 9)

void nm_ioctl(struct nmreq_header *hdr)
{
int fd = open("/dev/netmap", O_RDWR);

if (fd < 0) {
fprintf(stderr,"unable to open /dev/netmap\n");
exit(1);
}

if (ioctl(fd, NIOCCTRL, hdr) < 0) {
int err = errno;
close(fd);

if (hdr->nr_reqtype == NETMAP_REQ_VALE_LIST && err == ENOENT)
return;
else
fprintf(stderr,"netmap NIOCTRL\n");
exit(1);
}
}

static void
usage(int errcode)
{
fprintf(stderr,
"Return next free VALE port. Usage:\n"
"\t-i target VALE id (0 1 ..)\n");
exit(errcode);
}

int
is_number(const char *p)
{
const char *q;

if (*p == '\0')
return 0;
while (*p == '0')
p++;
for (q = p; *q != '\0'; q++)
if (! is_digit(*q))
return 0;
if (q - p > 10000 ) return 0;
return 1;
}

int
main(int argc, char *argv[])
{
struct nmreq_header hdr;
struct nmreq_vale_list req;
int i=0, j=0, x=0, sw_id;
char *token = NULL;
int target_vale_id = -1;
int ch, nr_cmd = 0, nr_arg = 0;
int port;
int switch_found;
int first_free = -1;
char myswitch[7]; // "vale99" max
int switch_ports[255];

while ((ch = getopt(argc, argv, "i:")) != -1) {
switch (ch) {
default:
fprintf(stderr, "bad option %c %s", ch, optarg);
usage(-1);
break;
case 'i':
target_vale_id = atoi(optarg);
if((target_vale_id<0)||(target_vale_id>99)) {
fprintf(stderr, "id not in range 0-99: %d\n", target_vale_id);
exit(-1);
}
break;
}
}

if (optind != argc) {
// fprintf(stderr, "optind %d argc %d\n", optind, argc);
usage(-1);
}

fprintf(stderr,"Search in vale name: %d\n",target_vale_id);

sprintf(myswitch,"vale%d",target_vale_id);
fprintf(stderr,"Search for SW: [%s]\n",myswitch);

for (i=0;i<255;i++)
switch_ports[i]=0;

//scan for 255 switch ID
for (sw_id = 0; sw_id < 64; sw_id++) {
memset(&hdr, 0, sizeof(hdr));

hdr.nr_version = NETMAP_API;
hdr.nr_reqtype = NETMAP_REQ_VALE_LIST;
hdr.nr_body = (uintptr_t)&req;
req.nr_bridge_idx = sw_id;
req.nr_port_idx = 0;

nm_ioctl(&hdr);

if (hdr.nr_name[0] == 0) {
fprintf(stderr, "no such vale id: %d\n", sw_id);
continue;
}

fprintf(stderr,"bridge ID: [vale%d]\n",sw_id);

//scan for 255 ports per SW
for (i = 0; i < 255; i++) {
switch_found=0;
memset(&hdr, 0, sizeof(hdr));

hdr.nr_version = NETMAP_API;
hdr.nr_reqtype = NETMAP_REQ_VALE_LIST;
hdr.nr_body = (uintptr_t)&req;
req.nr_bridge_idx = sw_id;
req.nr_port_idx = i;

nm_ioctl(&hdr);

if (hdr.nr_name[0] == 0 || req.nr_port_idx != i) {
break;
}

for (j = 0, token = strtok(hdr.nr_name, ":"); token; j++, token = strtok(NULL, ":")) {
switch(j) {
case 0:
fprintf(stderr,"switch name: %s\n", token);
if(!strcmp(token,myswitch)) {
fprintf(stderr," switch found!\n");
switch_found=1;
} else {
fprintf(stderr," switch NOT found ([%s][%s]!\n",token,myswitch);
switch_found=0;
}
break;
;;
case 1:
if(switch_found==0)
continue;
fprintf(stderr," port name: %s\n",token);
if (! is_number(token)) {
fprintf(stderr," not number, skip: %s\n", token);
continue;
}
port=atoi(token);
if(port==i) {
switch_ports[i]=1;
fprintf(stderr," already in use: %d\n", port);
continue;

}
break;
;;
default:
fprintf(stderr," unknown config: %s\n",token);
break;
}
}
}
}
fprintf(stderr,"SWITCH MAP:\n");
for (i=0;i<255;i++) {
if((switch_ports[i]==0)&&(first_free<0))
first_free=i;
fprintf(stderr,"%d ",switch_ports[i]);
}
fprintf(stderr,"\n");
if(first_free > -1 ) {
printf("%d",first_free);
exit(0);
}

exit(1);
}
12 changes: 12 additions & 0 deletions share/local-vale_ports.schema
@@ -0,0 +1,12 @@
# Default SQL scheme for DB local::vale_ports
#MYTABLE="vale_ports"

MYCOL="idx jname vale_id port_id"

idx="INTEGER PRIMARY KEY AUTOINCREMENT"
jname="TEXT DEFAULT ''"
vale_id="INTEGER DEFAULT 0" # idx from vale table
port_id="INTEGER DEFAULT 0"

INITDB=
CONSTRAINT=
5 changes: 4 additions & 1 deletion sudoexec/bstart
Expand Up @@ -37,7 +37,7 @@ debug_engine= # reset debug_engine before init
. ${system}
. ${distdir}/universe.subr
. ${distdir}/bhyve.subr
. ${distdir}/vnet.subr # get_vm_uplink_interface
. ${distdir}/vnet.subr # for get_vm_uplink_interface

[ -z "${jname}" -a -z "$*" ] && err 1 "${N1_COLOR}No bhyve specified${N0_COLOR}"

Expand Down Expand Up @@ -311,6 +311,9 @@ start_bhyve()
# truncate pcibus_run table
cbsdsqlrw ${jailsysdir}/${jname}/local.sqlite DELETE FROM pcibus_run

# delete orphaned vale_ports if exists
cbsdsqlrw local "DELETE FROM vale_ports WHERE jname=\"${jname}\""

# init bhyve_cpus
if ! compile_bhyve_cpus_args; then
${ECHO} "${N1_COLOR}Unable to compile bhyve_cpus_args for VMs: ${N2_COLOR}${jname}${N0_COLOR}"
Expand Down
3 changes: 3 additions & 0 deletions sudoexec/bstop
Expand Up @@ -174,6 +174,9 @@ jswmode jname=${jname} mode=master comment='0'
. ${vimageconf}

fwcounters jname=${jname} mode=remove

# delete orphaned vale_ports if exists
#cbsdsqlrw local "DELETE FROM vale_ports WHERE jname=\"${jname}\""
bcleanup jname=${jname}

# update state_time
Expand Down
4 changes: 4 additions & 0 deletions sudoexec/initenv
Expand Up @@ -377,6 +377,9 @@ phase2()
fi

if [ "${platform}" != "DragonFly" ]; then
if [ -f ${distdir}/misc/src/next-vale-port.c ]; then
${CC_CMD} -o ${distdir}/misc/next-vale-port ${distdir}/misc/src/next-vale-port.c
fi
[ ! -f "${distdir}/tools/vale-ctl" ] && ${CC_CMD} ${distdir}/tools/src/vale-ctl.c -o ${distdir}/tools/vale-ctl
[ ! -f "${distdir}/tools/nic_info" ] && ${CC_CMD} ${distdir}/tools/src/nic_info.c -o ${distdir}/tools/nic_info
[ ! -f "${distdir}/tools/bridge" ] && ${CC_CMD} ${distdir}/tools/src/bridge.c -o ${distdir}/tools/bridge
Expand Down Expand Up @@ -633,6 +636,7 @@ phase5()

env workdir=${workdir} /usr/local/bin/cbsd ${miscdir}/updatesql ${dbdir}/inv.${nodename}.sqlite ${distdir}/share/local-bsdsrc.schema bsdsrc
env workdir=${workdir} /usr/local/bin/cbsd ${miscdir}/updatesql ${dbdir}/inv.${nodename}.sqlite ${distdir}/share/local-vale.schema vale
env workdir=${workdir} /usr/local/bin/cbsd ${miscdir}/updatesql ${dbdir}/inv.${nodename}.sqlite ${distdir}/share/local-vale_ports.schema vale_ports

env workdir=${workdir} /usr/local/bin/cbsd ${miscdir}/updatesql ${dbdir}/inv.${nodename}.sqlite ${distdir}/share/forms.schema forms
env workdir=${workdir} /usr/local/bin/cbsd ${miscdir}/updatesql ${dbdir}/inv.${nodename}.sqlite ${distdir}/share/forms_system.schema system
Expand Down

0 comments on commit dcdfa38

Please sign in to comment.