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

Commit cf2267c

Browse files
committed
support setup interface command
Signed-off-by: Gao feng <[email protected]>
1 parent 42ba6da commit cf2267c

File tree

6 files changed

+128
-25
lines changed

6 files changed

+128
-25
lines changed

src/hyper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ enum {
3434
NEWCONTAINER,
3535
KILLCONTAINER,
3636
ONLINECPUMEM,
37+
SETUPINTERFACE,
3738
};
3839

3940
enum {

src/init.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,9 @@ static int hyper_channel_handle(struct hyper_event *de, uint32_t len)
11371137
case ONLINECPUMEM:
11381138
hyper_cmd_online_cpu_mem();
11391139
break;
1140+
case SETUPINTERFACE:
1141+
ret = hyper_cmd_setup_interface((char *)buf->data + 8, len - 8);
1142+
break;
11401143
default:
11411144
ret = -1;
11421145
break;

src/net.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "hyper.h"
1313
#include "util.h"
14+
#include "parse.h"
1415
#include "../config.h"
1516

1617
void hyper_set_be32(uint8_t *buf, uint32_t val)
@@ -783,6 +784,40 @@ void hyper_cleanup_network(struct hyper_pod *pod)
783784
netlink_close(&rth);
784785
}
785786

787+
int hyper_cmd_setup_interface(char *json, int length)
788+
{
789+
int ret = -1;
790+
struct hyper_interface *iface;
791+
struct rtnl_handle rth;
792+
793+
if (hyper_rescan() < 0)
794+
return -1;
795+
796+
if (netlink_open(&rth) < 0)
797+
return -1;
798+
799+
800+
iface = hyper_parse_setup_interface(json, length);
801+
if (iface == NULL) {
802+
fprintf(stderr, "parse interface failed\n");
803+
goto out;
804+
}
805+
ret = hyper_setup_interface(&rth, iface);
806+
if (ret < 0) {
807+
fprintf(stderr, "link up device %s failed\n", iface->device);
808+
goto out1;
809+
}
810+
ret = 0;
811+
out1:
812+
free(iface->device);
813+
free(iface->ipaddr);
814+
free(iface->mask);
815+
free(iface);
816+
out:
817+
netlink_close(&rth);
818+
return ret;
819+
}
820+
786821
int hyper_setup_dns(struct hyper_pod *pod)
787822
{
788823
int i, fd, ret = -1;

src/net.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ uint32_t hyper_get_be32(uint8_t *buf);
4747
void hyper_set_be64(uint8_t *buf, uint64_t val);
4848
uint64_t hyper_get_be64(uint8_t *buf);
4949
int hyper_setup_network(struct hyper_pod *pod);
50+
int hyper_cmd_setup_interface(char *json, int length);
5051
void hyper_cleanup_network(struct hyper_pod *pod);
5152
int hyper_setup_dns(struct hyper_pod *pod);
5253
void hyper_cleanup_dns(struct hyper_pod *pod);

src/parse.c

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,92 @@ static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *
635635
return -1;
636636
}
637637

638-
static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks)
638+
static int hyper_parse_interface(struct hyper_interface *iface,
639+
char *json, jsmntok_t *toks)
639640
{
640641
int i = 0, j, next_if;
642+
643+
if (toks[i].type != JSMN_OBJECT) {
644+
fprintf(stdout, "network array need object\n");
645+
return -1;
646+
}
647+
648+
next_if = toks[i].size;
649+
650+
i++;
651+
for (j = 0; j < next_if; j++, i++) {
652+
if (json_token_streq(json, &toks[i], "device")) {
653+
iface->device = (json_token_str(json, &toks[++i]));
654+
fprintf(stdout, "net device is %s\n", iface->device);
655+
} else if (json_token_streq(json, &toks[i], "ipAddress")) {
656+
iface->ipaddr = (json_token_str(json, &toks[++i]));
657+
fprintf(stdout, "net ipaddress is %s\n", iface->ipaddr);
658+
} else if (json_token_streq(json, &toks[i], "netMask")) {
659+
iface->mask = (json_token_str(json, &toks[++i]));
660+
fprintf(stdout, "net mask is %s\n", iface->mask);
661+
} else {
662+
fprintf(stderr, "get unknown section %s in interfaces\n",
663+
json_token_str(json, &toks[i]));
664+
goto fail;
665+
}
666+
}
667+
668+
return i;
669+
670+
fail:
671+
free(iface->device);
672+
free(iface->ipaddr);
673+
free(iface->mask);
674+
return -1;
675+
}
676+
677+
struct hyper_interface *hyper_parse_setup_interface(char *json, int length)
678+
{
679+
jsmn_parser p;
680+
int toks_num = 10, n;
681+
jsmntok_t *toks = NULL;
682+
683+
struct hyper_interface *iface = NULL;
684+
realloc:
685+
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
686+
if (toks == NULL) {
687+
fprintf(stderr, "allocate tokens for execcmd failed\n");
688+
goto fail;
689+
}
690+
691+
jsmn_init(&p);
692+
n = jsmn_parse(&p, json, length, toks, toks_num);
693+
if (n < 0) {
694+
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
695+
if (n == JSMN_ERROR_NOMEM) {
696+
toks_num *= 2;
697+
goto realloc;
698+
}
699+
goto out;
700+
}
701+
702+
iface = calloc(1, sizeof(*iface));
703+
if (iface == NULL) {
704+
fprintf(stderr, "allocate memory for interface failed\n");
705+
goto out;
706+
}
707+
708+
if (hyper_parse_interface(iface, json, toks) < 0) {
709+
fprintf(stderr, "allocate memory for interface failed\n");
710+
goto fail;
711+
}
712+
out:
713+
free(toks);
714+
return iface;
715+
fail:
716+
free(iface);
717+
iface = NULL;
718+
goto out;
719+
}
720+
721+
static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks)
722+
{
723+
int i = 0, j, next;
641724
struct hyper_interface *iface;
642725

643726
if (toks[i].type != JSMN_ARRAY) {
@@ -656,32 +739,11 @@ static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *
656739

657740
i++;
658741
for (j = 0; j < pod->i_num; j++) {
659-
int i_if;
660-
iface = &pod->iface[j];
661-
662-
if (toks[i].type != JSMN_OBJECT) {
663-
fprintf(stdout, "network array need object\n");
742+
next = hyper_parse_interface(&pod->iface[j], json, toks + i);
743+
if (next < 0)
664744
return -1;
665-
}
666-
next_if = toks[i].size;
667745

668-
i++;
669-
for (i_if = 0; i_if < next_if; i_if++, i++) {
670-
if (json_token_streq(json, &toks[i], "device")) {
671-
iface->device = (json_token_str(json, &toks[++i]));
672-
fprintf(stdout, "net device is %s\n", iface->device);
673-
} else if (json_token_streq(json, &toks[i], "ipAddress")) {
674-
iface->ipaddr = (json_token_str(json, &toks[++i]));
675-
fprintf(stdout, "net ipaddress is %s\n", iface->ipaddr);
676-
} else if (json_token_streq(json, &toks[i], "netMask")) {
677-
iface->mask = (json_token_str(json, &toks[++i]));
678-
fprintf(stdout, "net mask is %s\n", iface->mask);
679-
} else {
680-
fprintf(stderr, "get unknown section %s in interfaces\n",
681-
json_token_str(json, &toks[i]));
682-
return -1;
683-
}
684-
}
746+
i += next;
685747
}
686748

687749
return i;

src/parse.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length
1414
int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length);
1515
struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *json, int length);
1616
void hyper_free_container(struct hyper_container *c);
17+
struct hyper_interface *hyper_parse_setup_interface(char *json, int length);
1718

1819
#endif

0 commit comments

Comments
 (0)