Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 6dd63a0

Browse files
committed
use netlink to wait for network interfaces
Signed-off-by: Peng Tao <bergwolf@gmail.com>
1 parent e47ae41 commit 6dd63a0

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,10 +1192,10 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
11921192
hyper_cmd_online_cpu_mem();
11931193
break;
11941194
case SETUPINTERFACE:
1195-
ret = hyper_cmd_setup_interface((char *)buf->data + 8, len - 8);
1195+
ret = hyper_cmd_setup_interface((char *)buf->data + 8, len - 8, pod);
11961196
break;
11971197
case SETUPROUTE:
1198-
ret = hyper_cmd_setup_route((char *)buf->data + 8, len - 8);
1198+
ret = hyper_cmd_setup_route((char *)buf->data + 8, len - 8, pod);
11991199
break;
12001200
case SIGNALPROCESS:
12011201
ret = hyper_signal_process(pod, (char *)buf->data + 8, len - 8);

src/net.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "hyper.h"
1414
#include "util.h"
1515
#include "parse.h"
16+
#include "netlink.h"
1617
#include "../config.h"
1718

1819
void hyper_set_be32(uint8_t *buf, uint32_t val)
@@ -131,16 +132,19 @@ static int addattr_l(struct nlmsghdr *n, int maxlen, int type, void *data, int a
131132
return 0;
132133
}
133134

134-
static int hyper_get_ifindex(char *nic)
135+
static int hyper_get_ifindex(char *nic, struct hyper_pod *pod)
135136
{
136137
int fd, ifindex = -1;
137138
char path[512], buf[8];
138139

139140
sprintf(path, "/sys/class/net/%s/ifindex", nic);
140141
fprintf(stdout, "net device sys path is %s\n", path);
141142

142-
while (access(path, R_OK) < 0) {
143-
sched_yield();
143+
if (access(path, R_OK) < 0) {
144+
sprintf(path, "/net/%s/", nic);
145+
if (hyper_netlink_wait_dev(pod->ueventfd, path) < 0)
146+
return -1;
147+
sprintf(path, "/sys/class/net/%s/ifindex", nic);
144148
}
145149

146150
fd = open(path, O_RDONLY);
@@ -291,7 +295,8 @@ static int get_netmask(unsigned *val, const char *addr)
291295
}
292296

293297
static int hyper_setup_route(struct rtnl_handle *rth,
294-
struct hyper_route *rt)
298+
struct hyper_route *rt,
299+
struct hyper_pod *pod)
295300
{
296301
uint32_t data;
297302
struct {
@@ -330,7 +335,7 @@ static int hyper_setup_route(struct rtnl_handle *rth,
330335
}
331336

332337
if (rt->device) {
333-
int ifindex = hyper_get_ifindex(rt->device);
338+
int ifindex = hyper_get_ifindex(rt->device, pod);
334339
if (ifindex < 0) {
335340
fprintf(stderr, "failed to get the ifindix of %s\n", rt->device);
336341
return -1;
@@ -444,7 +449,8 @@ static int hyper_set_interface_mtu(struct rtnl_handle *rth,
444449
}
445450

446451
static int hyper_setup_interface(struct rtnl_handle *rth,
447-
struct hyper_interface *iface)
452+
struct hyper_interface *iface,
453+
struct hyper_pod *pod)
448454
{
449455
uint8_t data[4];
450456
unsigned mask;
@@ -467,7 +473,7 @@ static int hyper_setup_interface(struct rtnl_handle *rth,
467473
req.n.nlmsg_type = RTM_NEWADDR;
468474
req.ifa.ifa_family = AF_INET;
469475

470-
ifindex = hyper_get_ifindex(iface->device);
476+
ifindex = hyper_get_ifindex(iface->device, pod);
471477
if (ifindex < 0) {
472478
fprintf(stderr, "failed to get the ifindix of %s\n", iface->device);
473479
return -1;
@@ -557,7 +563,7 @@ int hyper_setup_network(struct hyper_pod *pod)
557563
for (i = 0; i < pod->i_num; i++) {
558564
iface = &pod->iface[i];
559565

560-
ret = hyper_setup_interface(&rth, iface);
566+
ret = hyper_setup_interface(&rth, iface, pod);
561567
if (ret < 0) {
562568
fprintf(stderr, "link up device %s failed\n", iface->device);
563569
goto out;
@@ -573,7 +579,7 @@ int hyper_setup_network(struct hyper_pod *pod)
573579
for (i = 0; i < pod->r_num; i++) {
574580
rt = &pod->rt[i];
575581

576-
ret = hyper_setup_route(&rth, rt);
582+
ret = hyper_setup_route(&rth, rt, pod);
577583
if (ret < 0) {
578584
fprintf(stderr, "setup route failed\n");
579585
goto out;
@@ -585,7 +591,7 @@ int hyper_setup_network(struct hyper_pod *pod)
585591
return ret;
586592
}
587593

588-
int hyper_cmd_setup_interface(char *json, int length)
594+
int hyper_cmd_setup_interface(char *json, int length, struct hyper_pod *pod)
589595
{
590596
int ret = -1;
591597
struct hyper_interface *iface;
@@ -603,7 +609,7 @@ int hyper_cmd_setup_interface(char *json, int length)
603609
fprintf(stderr, "parse interface failed\n");
604610
goto out;
605611
}
606-
ret = hyper_setup_interface(&rth, iface);
612+
ret = hyper_setup_interface(&rth, iface, pod);
607613
if (ret < 0) {
608614
fprintf(stderr, "link up device %s failed\n", iface->device);
609615
goto out1;
@@ -617,7 +623,8 @@ int hyper_cmd_setup_interface(char *json, int length)
617623
return ret;
618624
}
619625

620-
int hyper_cmd_setup_route(char *json, int length) {
626+
int hyper_cmd_setup_route(char *json, int length, struct hyper_pod *pod)
627+
{
621628
struct hyper_route *rts = NULL;
622629
int i, ret = -1;
623630
uint32_t r_num;
@@ -632,7 +639,7 @@ int hyper_cmd_setup_route(char *json, int length) {
632639
}
633640

634641
for (i = 0; i < r_num; i++) {
635-
ret = hyper_setup_route(&rth, &rts[i]);
642+
ret = hyper_setup_route(&rth, &rts[i], pod);
636643
if (ret < 0) {
637644
fprintf(stderr, "setup route failed\n");
638645
goto out;

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ uint32_t hyper_get_be32(uint8_t *buf);
4646
void hyper_set_be64(uint8_t *buf, uint64_t val);
4747
uint64_t hyper_get_be64(uint8_t *buf);
4848
int hyper_setup_network(struct hyper_pod *pod);
49-
int hyper_cmd_setup_interface(char *json, int length);
50-
int hyper_cmd_setup_route(char *json, int length);
49+
int hyper_cmd_setup_interface(char *json, int length, struct hyper_pod *pod);
50+
int hyper_cmd_setup_route(char *json, int length, struct hyper_pod *pod);
5151
int hyper_setup_dns(struct hyper_pod *pod);
5252
int hyper_setup_hostname(struct hyper_pod *pod);
5353
int hyper_send_data_block(int fd, uint8_t *data, uint32_t len);

0 commit comments

Comments
 (0)