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

Commit 67b91ca

Browse files
authored
Merge pull request #318 from WeiZhang555/add-network-functions-up
Enhance network functions
2 parents b0bfa56 + c00bb7b commit 67b91ca

File tree

4 files changed

+135
-41
lines changed

4 files changed

+135
-41
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: 130 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,64 @@ static int hyper_set_interface_attr(struct rtnl_handle *rth,
423423
return 0;
424424
}
425425

426+
static int hyper_set_interface_ipaddrs(struct rtnl_handle *rth,
427+
int ifindex,
428+
struct list_head* ipaddresses){
429+
uint8_t data[4];
430+
unsigned mask;
431+
struct {
432+
struct nlmsghdr n;
433+
struct ifaddrmsg ifa;
434+
char buf[256];
435+
} req;
436+
struct hyper_ipaddress *ip;
437+
438+
memset(&req, 0, sizeof(req));
439+
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
440+
req.ifa.ifa_family = AF_INET;
441+
req.ifa.ifa_index = ifindex;
442+
req.ifa.ifa_scope = 0;
443+
444+
list_for_each_entry(ip, ipaddresses, list) {
445+
if (ip->addr[0]=='\0'){
446+
continue;
447+
} else if (ip->addr[0]=='-') {
448+
//start with '-' means delete an existing address
449+
req.n.nlmsg_flags = NLM_F_REQUEST;
450+
req.n.nlmsg_type = RTM_DELADDR;;
451+
if (get_addr_ipv4((uint8_t *)&data, &ip->addr[1]) <= 0) {
452+
fprintf(stderr, "get addr failed\n");
453+
return -1;
454+
}
455+
} else{
456+
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
457+
req.n.nlmsg_type = RTM_NEWADDR;
458+
if (get_addr_ipv4((uint8_t *)&data, ip->addr) <= 0) {
459+
fprintf(stderr, "get addr failed\n");
460+
return -1;
461+
}
462+
}
463+
464+
if (addattr_l(&req.n, sizeof(req), IFA_LOCAL, &data, 4)) {
465+
fprintf(stderr, "setup attr failed\n");
466+
return -1;
467+
}
468+
469+
if (get_netmask(&mask, ip->mask) < 0) {
470+
fprintf(stderr, "get netamsk failed\n");
471+
return -1;
472+
}
473+
474+
req.ifa.ifa_prefixlen = mask;
475+
fprintf(stdout, "interface get netamsk %d %s\n", req.ifa.ifa_prefixlen, ip->mask);
476+
if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) {
477+
perror("rtnl_talk failed");
478+
return -1;
479+
}
480+
}
481+
return 0;
482+
}
483+
426484
static int hyper_set_interface_name(struct rtnl_handle *rth,
427485
int ifindex,
428486
char *new_device_name)
@@ -452,56 +510,22 @@ static int hyper_setup_interface(struct rtnl_handle *rth,
452510
struct hyper_interface *iface,
453511
struct hyper_pod *pod)
454512
{
455-
uint8_t data[4];
456-
unsigned mask;
457-
struct {
458-
struct nlmsghdr n;
459-
struct ifaddrmsg ifa;
460-
char buf[256];
461-
} req;
462513
int ifindex;
463-
struct hyper_ipaddress *ip;
464-
465-
if (!iface->device || list_empty(&iface->ipaddresses)) {
466-
fprintf(stderr, "interface information incorrect\n");
514+
if (!iface->device) {
515+
fprintf(stderr, "device name can't be empty\n");
467516
return -1;
468517
}
469518

470-
memset(&req, 0, sizeof(req));
471-
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
472-
req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
473-
req.n.nlmsg_type = RTM_NEWADDR;
474-
req.ifa.ifa_family = AF_INET;
475-
476519
ifindex = hyper_get_ifindex(iface->device, pod);
477520
if (ifindex < 0) {
478521
fprintf(stderr, "failed to get the ifindix of %s\n", iface->device);
479522
return -1;
480523
}
481524

482-
req.ifa.ifa_index = ifindex;
483-
req.ifa.ifa_scope = 0;
484-
485-
list_for_each_entry(ip, &iface->ipaddresses, list) {
486-
if (get_addr_ipv4((uint8_t *)&data, ip->addr) <= 0) {
487-
fprintf(stderr, "get addr failed\n");
488-
return -1;
489-
}
490-
491-
if (addattr_l(&req.n, sizeof(req), IFA_LOCAL, &data, 4)) {
492-
fprintf(stderr, "setup attr failed\n");
493-
return -1;
494-
}
495-
496-
if (get_netmask(&mask, ip->mask) < 0) {
497-
fprintf(stderr, "get netamsk failed\n");
498-
return -1;
499-
}
500-
501-
req.ifa.ifa_prefixlen = mask;
502-
fprintf(stdout, "interface get netamsk %d %s\n", req.ifa.ifa_prefixlen, ip->mask);
503-
if (rtnl_talk(rth, &req.n, 0, 0, NULL) < 0) {
504-
perror("rtnl_talk failed");
525+
if (!list_empty(&iface->ipaddresses)) {
526+
if (hyper_set_interface_ipaddrs(rth, ifindex, &iface->ipaddresses) < 0) {
527+
fprintf(stderr, "set ip addresses failed for interface %s\n",
528+
iface->device);
505529
return -1;
506530
}
507531
}
@@ -603,7 +627,6 @@ int hyper_cmd_setup_interface(char *json, int length, struct hyper_pod *pod)
603627
if (netlink_open(&rth) < 0)
604628
return -1;
605629

606-
607630
iface = hyper_parse_setup_interface(json, length);
608631
if (iface == NULL) {
609632
fprintf(stderr, "parse interface failed\n");
@@ -623,6 +646,71 @@ int hyper_cmd_setup_interface(char *json, int length, struct hyper_pod *pod)
623646
return ret;
624647
}
625648

649+
static int hyper_remove_nic(char *device)
650+
{
651+
char path[256], real[128];
652+
int fd;
653+
ssize_t size;
654+
655+
sprintf(path, "/sys/class/net/%s", device);
656+
657+
size = readlink(path, real, 128);
658+
if (size < 0 || size > 127) {
659+
perror("fail to read link directory");
660+
return -1;
661+
}
662+
663+
real[size] = '\0';
664+
sprintf(path, "/sys/%s/../../../remove", real + 5);
665+
666+
fprintf(stdout, "get net sys path %s\n", path);
667+
668+
fd = open(path, O_WRONLY);
669+
if (fd < 0) {
670+
perror("open file failed");
671+
return -1;
672+
}
673+
674+
if (write(fd, "1\n", 2) < 0) {
675+
perror("write 1 to file failed");
676+
close(fd);
677+
return 1;
678+
}
679+
680+
close(fd);
681+
return 0;
682+
}
683+
684+
int hyper_cmd_delete_interface(char *json, int length)
685+
{
686+
int ret = -1;
687+
struct hyper_interface *iface;
688+
struct rtnl_handle rth;
689+
690+
fprintf(stdout, "client demands to remove network interface\n");
691+
if (netlink_open(&rth) < 0)
692+
return -1;
693+
694+
iface = hyper_parse_setup_interface(json, length);
695+
if (iface == NULL) {
696+
fprintf(stderr, "parse interface failed\n");
697+
goto out;
698+
}
699+
700+
if (hyper_remove_nic(iface->device) < 0){
701+
fprintf(stderr, "remove device %s failed\n", iface->device);
702+
goto out1;
703+
}
704+
fprintf(stdout, "remove device %s successfully\n", iface->device);
705+
ret = 0;
706+
out1:
707+
hyper_free_interface(iface);
708+
free(iface);
709+
out:
710+
netlink_close(&rth);
711+
return ret;
712+
}
713+
626714
int hyper_cmd_setup_route(char *json, int length, struct hyper_pod *pod)
627715
{
628716
struct hyper_route *rts = NULL;
@@ -780,3 +868,4 @@ int hyper_setup_dns(struct hyper_pod *pod)
780868
close(fd);
781869
return ret;
782870
}
871+

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)