Skip to content

Commit 6c604cd

Browse files
committed
Add link_type to mimic show and lock file
1 parent 4b9efd0 commit 6c604cd

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

Diff for: common/defs.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,11 @@ enum link_type {
277277
static inline const char* link_type_str(enum link_type link) {
278278
switch (link) {
279279
case LINK_ETH:
280-
return N_("Ethernet");
280+
return "eth";
281281
case LINK_NONE:
282-
return N_("None");
282+
return "none";
283283
default:
284-
return N_("(unknown)");
284+
return "(unknown)";
285285
}
286286
}
287287

Diff for: src/config.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static int parse_bool(const char* str, __s16* result) {
127127
return 0;
128128
}
129129

130-
int parse_link(const char* str, enum link_type* link) {
130+
int parse_link_type(const char* str, enum link_type* link) {
131131
if (strcmp("eth", str) == 0)
132132
*link = LINK_ETH;
133133
else if (strcmp("none", str) == 0)
@@ -282,6 +282,9 @@ int parse_config_file(FILE* file, struct run_args* args) {
282282
}
283283
log_verbosity = parsed;
284284

285+
} else if (strcmp(k, "link_type") == 0) {
286+
try(parse_link_type(v, &args->link_type));
287+
285288
} else if (strcmp(k, "filter") == 0) {
286289
unsigned int fc = args->filter_count;
287290
ret = parse_filter(v, &args->filters[fc], &args->info[fc], sizeof_array(args->filters) - fc);
@@ -333,6 +336,8 @@ int parse_lock_file(FILE* file, struct lock_content* c) {
333336
version_checked = true;
334337
} else if (strcmp(k, "pid") == 0)
335338
try(parse_int_any(v, &c->pid));
339+
else if (strcmp(k, "link_type") == 0)
340+
try(parse_link_type(v, &c->link_type));
336341
else if (strcmp(k, "egress_id") == 0)
337342
try(parse_int_any(v, &c->egress_id));
338343
else if (strcmp(k, "ingress_id") == 0)
@@ -351,6 +356,7 @@ int parse_lock_file(FILE* file, struct lock_content* c) {
351356
int write_lock_file(int fd, const struct lock_content* c) {
352357
try(dprintf(fd, "version=%s\n", argp_program_version));
353358
try(dprintf(fd, "pid=%d\n", c->pid));
359+
try(dprintf(fd, "link_type=%s\n", link_type_str(c->link_type)));
354360
try(dprintf(fd, "egress_id=%d\n", c->egress_id));
355361
try(dprintf(fd, "ingress_id=%d\n", c->ingress_id));
356362
try(dprintf(fd, "whitelist_id=%d\n", c->whitelist_id));

Diff for: src/main.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ extern const struct argp show_argp;
4444

4545
int subcmd_run(struct run_args* args);
4646

47-
int show_overview(int ifindex, int whitelist_fd, struct filter_settings* gsettings,
48-
int log_verbosity);
47+
int show_overview(int ifindex, enum link_type link_type, int whitelist_fd,
48+
struct filter_settings* gsettings, int log_verbosity);
4949
int subcmd_show(struct show_args* args);
5050

5151
struct lock_content {
5252
pid_t pid;
53-
enum link_type link;
53+
enum link_type link_type;
5454
int egress_id, ingress_id;
5555
int whitelist_id, conns_id;
5656
struct filter_settings settings;
5757
};
5858

59-
int parse_link(const char* str, enum link_type* link);
59+
int parse_link_type(const char* str, enum link_type* link);
6060
int parse_handshake(char* str, struct filter_handshake* h);
6161
int parse_keepalive(char* str, struct filter_keepalive* k);
6262
int parse_padding(const char* str, __s16* padding);

Diff for: src/run.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static inline error_t args_parse_opt(int key, char* arg, struct argp_state* stat
8484
args->filter_count += ret;
8585
break;
8686
case 'L':
87-
try(parse_link(arg, &args->link_type));
87+
try(parse_link_type(arg, &args->link_type));
8888
break;
8989
case 'x':
9090
args->xdp_mode = try(parse_xdp_mode(arg));
@@ -592,6 +592,7 @@ static inline int run_bpf(struct run_args* args, int lock_fd, const char* ifname
592592
// Save state to lock file
593593
struct lock_content lock_content = {
594594
.pid = getpid(),
595+
.link_type = args->link_type,
595596
.egress_id = _get_prog_id(egress_handler),
596597
.ingress_id = _get_prog_id(ingress_handler),
597598
.whitelist_id = _get_map_id(mimic_whitelist),
@@ -622,7 +623,7 @@ static inline int run_bpf(struct run_args* args, int lock_fd, const char* ifname
622623
if (args->filter_count <= 0)
623624
log_warn(_("no filter specified"));
624625
else
625-
show_overview(ifindex, mimic_whitelist_fd, &args->gsettings, log_verbosity);
626+
show_overview(ifindex, args->link_type, mimic_whitelist_fd, &args->gsettings, log_verbosity);
626627

627628
struct epoll_event ev;
628629
struct epoll_event events[EPOLL_MAX_EVENTS];

Diff for: src/show.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@ const struct argp show_argp = {
5454
N_("\vSee mimic(1) for detailed usage."),
5555
};
5656

57-
int show_overview(int ifindex, int whitelist_fd, struct filter_settings* gs, int log_verbosity) {
57+
int show_overview(int ifindex, enum link_type link_type, int whitelist_fd,
58+
struct filter_settings* gs, int log_verbosity) {
5859
if (log_verbosity >= 0 && !LOG_ALLOW_INFO) return 0;
5960
FILE* out = log_verbosity >= 0 ? stderr : stdout;
6061

62+
if (LOG_ALLOW_INFO) fprintf(out, "%s%s " RESET, log_prefixes[2][0], log_prefixes[2][1]);
63+
fprintf(out, _(" %sLink Type:%s %s\n"), BOLD, RESET, link_type_str(link_type));
64+
6165
struct bpf_xdp_query_opts xdp_opts = {.sz = sizeof(xdp_opts)};
6266
try(bpf_xdp_query(ifindex, 0, &xdp_opts), _("failed to query XDP: %s"), strret);
6367
if (LOG_ALLOW_INFO) fprintf(out, "%s%s " RESET, log_prefixes[2][0], log_prefixes[2][1]);
@@ -173,7 +177,7 @@ int subcmd_show(struct show_args* args) {
173177
int whitelist_fd raii(closep) =
174178
try(bpf_map_get_fd_by_id(lock_content.whitelist_id), _("failed to get fd of map '%s': %s"),
175179
"mimic_whitelist", strret);
176-
show_overview(ifindex, whitelist_fd, &lock_content.settings, -1);
180+
show_overview(ifindex, lock_content.link_type, whitelist_fd, &lock_content.settings, -1);
177181
printf("\n");
178182
}
179183

0 commit comments

Comments
 (0)