1313#include "hyper.h"
1414#include "util.h"
1515#include "parse.h"
16+ #include "netlink.h"
1617#include "../config.h"
1718
1819void 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
293297static 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
446451static 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 ;
0 commit comments