@@ -623,6 +623,70 @@ int hyper_cmd_setup_interface(char *json, int length, struct hyper_pod *pod)
623623 return ret ;
624624}
625625
626+ static int hyper_remove_nic (char * device )
627+ {
628+ char path [256 ], real [128 ];
629+ int fd ;
630+ ssize_t size ;
631+
632+ sprintf (path , "/sys/class/net/%s" , device );
633+
634+ size = readlink (path , real , 128 );
635+ if (size < 0 || size > 127 ) {
636+ perror ("fail to read link directory" );
637+ return -1 ;
638+ }
639+
640+ real [size ] = '\0' ;
641+ sprintf (path , "/sys/%s/../../../remove" , real + 5 );
642+
643+ fprintf (stdout , "get net sys path %s\n" , path );
644+
645+ fd = open (path , O_WRONLY );
646+ if (fd < 0 ) {
647+ perror ("open file failed" );
648+ return -1 ;
649+ }
650+
651+ if (write (fd , "1\n" , 2 ) < 0 ) {
652+ perror ("write 1 to file failed" );
653+ close (fd );
654+ return 1 ;
655+ }
656+
657+ close (fd );
658+ return 0 ;
659+ }
660+ int hyper_cmd_delete_interface (char * json , int length )
661+ {
662+ int ret = -1 ;
663+ struct hyper_interface * iface ;
664+ struct rtnl_handle rth ;
665+
666+ fprintf (stdout , "client demands to remove network interface\n" );
667+ if (netlink_open (& rth ) < 0 )
668+ return -1 ;
669+
670+ iface = hyper_parse_setup_interface (json , length );
671+ if (iface == NULL ) {
672+ fprintf (stderr , "parse interface failed\n" );
673+ goto out ;
674+ }
675+
676+ if (hyper_remove_nic (iface -> device ) < 0 ){
677+ fprintf (stderr , "remove device %s failed\n" , iface -> device );
678+ goto out1 ;
679+ }
680+ fprintf (stdout , "remove device %s successfully\n" , iface -> device );
681+ ret = 0 ;
682+ out1 :
683+ hyper_free_interface (iface );
684+ free (iface );
685+ out :
686+ netlink_close (& rth );
687+ return ret ;
688+ }
689+
626690int hyper_cmd_setup_route (char * json , int length , struct hyper_pod * pod )
627691{
628692 struct hyper_route * rts = NULL ;
@@ -780,3 +844,4 @@ int hyper_setup_dns(struct hyper_pod *pod)
780844 close (fd );
781845 return ret ;
782846}
847+
0 commit comments