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

Commit 0fca641

Browse files
committed
Add NIC removal support
Add one new event for handling interface deletion request from runv Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
1 parent 8b70ff4 commit 0fca641

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

src/api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum {
3131
REMOVECONTAINER,
3232
PROCESSASYNCEVENT,
3333
SIGNALPROCESS,
34+
DELETEINTERFACE, // 25
3435
};
3536

3637
// "hyperstart" is the special container ID for adding processes.

src/init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,9 @@ static int hyper_ctlmsg_handle(struct hyper_event *he, uint32_t len)
12921292
case SETUPINTERFACE:
12931293
ret = hyper_cmd_setup_interface((char *)buf->data + 8, len - 8, pod);
12941294
break;
1295+
case DELETEINTERFACE:
1296+
ret = hyper_cmd_delete_interface((char *)buf->data + 8, len - 8);
1297+
break;
12951298
case SETUPROUTE:
12961299
ret = hyper_cmd_setup_route((char *)buf->data + 8, len - 8, pod);
12971300
break;

src/net.c

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
626690
int 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+

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ 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);
5454
int hyper_send_data(int fd, uint8_t *data, uint32_t len);
55+
int hyper_cmd_delete_interface(char *json, int length);
5556
#endif

0 commit comments

Comments
 (0)