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

Commit 185cfb9

Browse files
committed
Merge pull request #95 from gao-feng/interface
support setup interface command
2 parents c8b2fee + cf2267c commit 185cfb9

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
@@ -683,9 +683,92 @@ static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *
683683
return -1;
684684
}
685685

686-
static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks)
686+
static int hyper_parse_interface(struct hyper_interface *iface,
687+
char *json, jsmntok_t *toks)
687688
{
688689
int i = 0, j, next_if;
690+
691+
if (toks[i].type != JSMN_OBJECT) {
692+
fprintf(stdout, "network array need object\n");
693+
return -1;
694+
}
695+
696+
next_if = toks[i].size;
697+
698+
i++;
699+
for (j = 0; j < next_if; j++, i++) {
700+
if (json_token_streq(json, &toks[i], "device")) {
701+
iface->device = (json_token_str(json, &toks[++i]));
702+
fprintf(stdout, "net device is %s\n", iface->device);
703+
} else if (json_token_streq(json, &toks[i], "ipAddress")) {
704+
iface->ipaddr = (json_token_str(json, &toks[++i]));
705+
fprintf(stdout, "net ipaddress is %s\n", iface->ipaddr);
706+
} else if (json_token_streq(json, &toks[i], "netMask")) {
707+
iface->mask = (json_token_str(json, &toks[++i]));
708+
fprintf(stdout, "net mask is %s\n", iface->mask);
709+
} else {
710+
fprintf(stderr, "get unknown section %s in interfaces\n",
711+
json_token_str(json, &toks[i]));
712+
goto fail;
713+
}
714+
}
715+
716+
return i;
717+
718+
fail:
719+
free(iface->device);
720+
free(iface->ipaddr);
721+
free(iface->mask);
722+
return -1;
723+
}
724+
725+
struct hyper_interface *hyper_parse_setup_interface(char *json, int length)
726+
{
727+
jsmn_parser p;
728+
int toks_num = 10, n;
729+
jsmntok_t *toks = NULL;
730+
731+
struct hyper_interface *iface = NULL;
732+
realloc:
733+
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
734+
if (toks == NULL) {
735+
fprintf(stderr, "allocate tokens for execcmd failed\n");
736+
goto fail;
737+
}
738+
739+
jsmn_init(&p);
740+
n = jsmn_parse(&p, json, length, toks, toks_num);
741+
if (n < 0) {
742+
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
743+
if (n == JSMN_ERROR_NOMEM) {
744+
toks_num *= 2;
745+
goto realloc;
746+
}
747+
goto out;
748+
}
749+
750+
iface = calloc(1, sizeof(*iface));
751+
if (iface == NULL) {
752+
fprintf(stderr, "allocate memory for interface failed\n");
753+
goto out;
754+
}
755+
756+
if (hyper_parse_interface(iface, json, toks) < 0) {
757+
fprintf(stderr, "allocate memory for interface failed\n");
758+
goto fail;
759+
}
760+
out:
761+
free(toks);
762+
return iface;
763+
fail:
764+
free(iface);
765+
iface = NULL;
766+
goto out;
767+
}
768+
769+
static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks)
770+
{
771+
int i = 0, j, next;
689772
struct hyper_interface *iface;
690773

691774
if (toks[i].type != JSMN_ARRAY) {
@@ -704,32 +787,11 @@ static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *
704787

705788
i++;
706789
for (j = 0; j < pod->i_num; j++) {
707-
int i_if;
708-
iface = &pod->iface[j];
709-
710-
if (toks[i].type != JSMN_OBJECT) {
711-
fprintf(stdout, "network array need object\n");
790+
next = hyper_parse_interface(&pod->iface[j], json, toks + i);
791+
if (next < 0)
712792
return -1;
713-
}
714-
next_if = toks[i].size;
715793

716-
i++;
717-
for (i_if = 0; i_if < next_if; i_if++, i++) {
718-
if (json_token_streq(json, &toks[i], "device")) {
719-
iface->device = (json_token_str(json, &toks[++i]));
720-
fprintf(stdout, "net device is %s\n", iface->device);
721-
} else if (json_token_streq(json, &toks[i], "ipAddress")) {
722-
iface->ipaddr = (json_token_str(json, &toks[++i]));
723-
fprintf(stdout, "net ipaddress is %s\n", iface->ipaddr);
724-
} else if (json_token_streq(json, &toks[i], "netMask")) {
725-
iface->mask = (json_token_str(json, &toks[++i]));
726-
fprintf(stdout, "net mask is %s\n", iface->mask);
727-
} else {
728-
fprintf(stderr, "get unknown section %s in interfaces\n",
729-
json_token_str(json, &toks[i]));
730-
return -1;
731-
}
732-
}
794+
i += next;
733795
}
734796

735797
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)