Skip to content

Commit c1ae28f

Browse files
zhengchaoshaoKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
samples/bpf: detach xdp prog when program exits unexpectedly in xdp_rxq_info_user
When xdp_rxq_info_user program exits unexpectedly, it doesn't detach xdp prog of device, and other xdp prog can't be attached to the device. So call init_exit() to detach xdp prog when program exits unexpectedly. Signed-off-by: Zhengchao Shao <[email protected]>
1 parent 8c3b780 commit c1ae28f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

samples/bpf/xdp_rxq_info_user.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n"
1717
#include <getopt.h>
1818
#include <net/if.h>
1919
#include <time.h>
20-
20+
#include <limits.h>
2121
#include <arpa/inet.h>
2222
#include <linux/if_link.h>
2323

@@ -43,6 +43,9 @@ static struct bpf_map *rx_queue_index_map;
4343
#define EXIT_FAIL_BPF 4
4444
#define EXIT_FAIL_MEM 5
4545

46+
#define FAIL_MEM_SIG INT_MAX
47+
#define FAIL_STAT_SIG (INT_MAX - 1)
48+
4649
static const struct option long_options[] = {
4750
{"help", no_argument, NULL, 'h' },
4851
{"dev", required_argument, NULL, 'd' },
@@ -76,6 +79,12 @@ static void int_exit(int sig)
7679
printf("program on interface changed, not removing\n");
7780
}
7881
}
82+
83+
if (sig == FAIL_MEM_SIG)
84+
exit(EXIT_FAIL_MEM);
85+
else if (sig == FAIL_STAT_SIG)
86+
exit(EXIT_FAIL);
87+
7988
exit(EXIT_OK);
8089
}
8190

@@ -140,7 +149,8 @@ static char* options2str(enum cfg_options_flags flag)
140149
if (flag & READ_MEM)
141150
return "read";
142151
fprintf(stderr, "ERR: Unknown config option flags");
143-
exit(EXIT_FAIL);
152+
int_exit(FAIL_STAT_SIG);
153+
return "unknown";
144154
}
145155

146156
static void usage(char *argv[])
@@ -173,7 +183,7 @@ static __u64 gettime(void)
173183
res = clock_gettime(CLOCK_MONOTONIC, &t);
174184
if (res < 0) {
175185
fprintf(stderr, "Error with gettimeofday! (%i)\n", res);
176-
exit(EXIT_FAIL);
186+
int_exit(FAIL_STAT_SIG);
177187
}
178188
return (__u64) t.tv_sec * NANOSEC_PER_SEC + t.tv_nsec;
179189
}
@@ -201,7 +211,7 @@ static struct datarec *alloc_record_per_cpu(void)
201211
array = calloc(nr_cpus, sizeof(struct datarec));
202212
if (!array) {
203213
fprintf(stderr, "Mem alloc error (nr_cpus:%u)\n", nr_cpus);
204-
exit(EXIT_FAIL_MEM);
214+
int_exit(FAIL_MEM_SIG);
205215
}
206216
return array;
207217
}
@@ -214,7 +224,7 @@ static struct record *alloc_record_per_rxq(void)
214224
array = calloc(nr_rxqs, sizeof(struct record));
215225
if (!array) {
216226
fprintf(stderr, "Mem alloc error (nr_rxqs:%u)\n", nr_rxqs);
217-
exit(EXIT_FAIL_MEM);
227+
int_exit(FAIL_MEM_SIG);
218228
}
219229
return array;
220230
}
@@ -228,7 +238,7 @@ static struct stats_record *alloc_stats_record(void)
228238
rec = calloc(1, sizeof(struct stats_record));
229239
if (!rec) {
230240
fprintf(stderr, "Mem alloc error\n");
231-
exit(EXIT_FAIL_MEM);
241+
int_exit(FAIL_MEM_SIG);
232242
}
233243
rec->rxq = alloc_record_per_rxq();
234244
for (i = 0; i < nr_rxqs; i++)

0 commit comments

Comments
 (0)