Skip to content

Commit bbbb8f5

Browse files
authored
Merge pull request #79 from insa-unyte/development
Development
2 parents 478b4da + 27d991c commit bbbb8f5

16 files changed

+137
-207
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ client_ebpf_user
1919
vmlinux.h
2020

2121
docker/tmp
22+
.DS_Store
2223

2324
**/*.log
2425
**/logs/run*

README.md

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@ See [INSTALL](INSTALL.md)
99
The collector allows to read and parse UDP-notif protocol messages from a ip/port specified on the parameters. It allows to get directly the buffer and the metadata of the message in a struct.
1010

1111
The api is in `unyte_udp_collector.h`:
12-
- `unyte_udp_collector_t *unyte_udp_start_collector(unyte_udp_options_t *options)` from `unyte_udp_collector.h`: Initialize the UDP-notif messages collector. It accepts a struct with different options: address (the IP address to listen to), port (port to listen to), recvmmsg_vlen (vlen used on recvmmsg syscall meaning how many messages to receive on every syscall, by default 10)
13-
- `unyte_udp_collector_t *unyte_udp_start_collector_sk(unyte_udp_sk_options_t *options)` from `unyte_udp_collector.h`: Initialize the UDP-notif messages collector binded to a socket. It accepts a struct with different options: socket file descriptor to listen on, recvmmsg_vlen (vlen used on recvmmsg syscall meaning how many messages to receive on every syscall, by default 10)
12+
- `int unyte_udp_create_socket(char *address, char *port, uint64_t buffer_size)` from `unyte_udp_utils.h`: Helper that creates and binds a socket to an address and port.
13+
- `unyte_udp_collector_t *unyte_udp_start_collector(unyte_udp_options_t *options)` from `unyte_udp_collector.h`: Initialize the UDP-notif messages collector. It accepts a struct with different options: socketfd of the socket to listen to, recvmmsg_vlen (vlen used on recvmmsg syscall meaning how many messages to receive on every syscall, by default 10)...
1414
- `void *unyte_udp_queue_read(unyte_udp_queue_t *queue)` from `unyte_udp_queue.h` : read from a queue a struct with all the message buffer and metadata.
1515
- `int unyte_udp_free_all(unyte_seg_met_t *seg)` from `unyte_udp_collector.h`: free all struct used on a message received.
1616

1717
Simple example of usage :
1818
```c
1919
#include <stdio.h>
20-
#include <stdlib.h>
21-
#include <pthread.h>
22-
#include <stdint.h>
23-
#include <string.h>
24-
#include <signal.h>
2520
#include <unistd.h>
21+
#include <arpa/inet.h>
2622

2723
// include installed library headers
2824
#include <unyte-udp-notif/unyte_udp_collector.h>
@@ -34,10 +30,13 @@ Simple example of usage :
3430

3531
int main()
3632
{
33+
// Initialize socket and bind it to the address
34+
int socketfd = unyte_udp_create_socket(ADDR, PORT, DEFAULT_SK_BUFF_SIZE);
35+
3736
// Initialize collector options
3837
unyte_udp_options_t options = {0};
39-
options.address = ADDR;
40-
options.port = PORT;
38+
// add socket fd reference to options
39+
options.socket_fd = socketfd
4140
// if argument set to 0, defaults are used
4241
options.recvmmsg_vlen = 0; // vlen parameter for recvmmsg. Default: 10
4342
options.output_queue_size = 0; // output queue size. Default: 1000
@@ -113,8 +112,7 @@ To activate this thread, you must initiate the monitoring thread queue size (`mo
113112
```c
114113
typedef struct
115114
{
116-
char *address;
117-
char *port;
115+
int socket_fd; // socket file descriptor
118116
...
119117
uint monitoring_queue_size; // monitoring queue size if wanted to activate the monitoring thread. Default: 0. Recommended: 500.
120118
uint monitoring_delay; // monitoring queue frequence in seconds. Default: 5 seconds
@@ -155,7 +153,6 @@ typedef struct unyte_message
155153

156154
Simple usage of the sender :
157155
```c
158-
#include <pthread.h>
159156
#include <stdio.h>
160157
#include <stdlib.h>
161158

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.0-git
1+
0.5.1-git

examples/client_monitoring.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <pthread.h>
4-
#include <stdint.h>
5-
#include <string.h>
6-
#include <signal.h>
72
#include <unistd.h>
3+
#include <arpa/inet.h>
84

95
#include "../src/hexdump.h"
106
#include "../src/unyte_udp_collector.h"
@@ -42,17 +38,18 @@ int main(int argc, char *argv[])
4238
exit(1);
4339
}
4440

41+
// Create a udp socket with default socket buffer
42+
int socketfd = unyte_udp_create_socket(argv[1], argv[2], DEFAULT_SK_BUFF_SIZE);
43+
printf("Listening on %s:%s\n", argv[1], argv[2]);
44+
4545
// Initialize collector options
4646
unyte_udp_options_t options = {0};
47-
options.address = argv[1];
48-
options.port = argv[2];
4947
options.recvmmsg_vlen = USED_VLEN;
48+
options.socket_fd = socketfd; // passing socket file descriptor to listen to
5049
options.monitoring_delay = 2;
5150
options.monitoring_queue_size = 500;
5251

53-
printf("Listening on %s:%s\n", options.address, options.port);
54-
55-
/* Initialize collector */
52+
// Initialize collector
5653
unyte_udp_collector_t *collector = unyte_udp_start_collector(&options);
5754
int recv_count = 0;
5855
int max = MAX_TO_RECEIVE;

examples/client_sample.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <pthread.h>
4-
#include <stdint.h>
5-
#include <string.h>
6-
#include <signal.h>
72
#include <unistd.h>
83
#include <arpa/inet.h>
94

@@ -24,12 +19,14 @@ int main(int argc, char *argv[])
2419
exit(1);
2520
}
2621

22+
// Create a udp socket with default socket buffer
23+
int socketfd = unyte_udp_create_socket(argv[1], argv[2], DEFAULT_SK_BUFF_SIZE);
24+
printf("Listening on %s:%s\n", argv[1], argv[2]);
25+
2726
// Initialize collector options
2827
unyte_udp_options_t options = {0};
2928
options.recvmmsg_vlen = USED_VLEN;
30-
options.address = argv[1];
31-
options.port = argv[2];
32-
printf("Listening on %s:%s\n", options.address, options.port);
29+
options.socket_fd = socketfd; // passing socket file descriptor to listen to
3330

3431
/* Initialize collector */
3532
unyte_udp_collector_t *collector = unyte_udp_start_collector(&options);

examples/client_socket.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <pthread.h>
4-
#include <stdint.h>
52
#include <string.h>
6-
#include <signal.h>
73
#include <unistd.h>
84
#include <arpa/inet.h>
95
#include <netdb.h>
@@ -19,7 +15,7 @@
1915
/**
2016
* Creates own custom socket
2117
*/
22-
int create_socket(char *address, char *port)
18+
int create_custom_socket(char *address, char *port)
2319
{
2420
struct addrinfo *addr_info;
2521
struct addrinfo hints;
@@ -88,16 +84,16 @@ int main(int argc, char *argv[])
8884
exit(1);
8985
}
9086

91-
int sockfd = create_socket(argv[1], argv[2]);
87+
int sockfd = create_custom_socket(argv[1], argv[2]);
9288

9389
// Initialize collector options
94-
unyte_udp_sk_options_t options = {0};
90+
unyte_udp_options_t options = {0};
9591
options.recvmmsg_vlen = USED_VLEN;
9692
options.socket_fd = sockfd;
9793
printf("Listening on socket %d\n", options.socket_fd);
9894

9995
/* Initialize collector */
100-
unyte_udp_collector_t *collector = unyte_udp_start_collector_sk(&options);
96+
unyte_udp_collector_t *collector = unyte_udp_start_collector(&options);
10197
int recv_count = 0;
10298
int max = MAX_TO_RECEIVE;
10399

examples/eBPF/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ To build this directory, use the following options on `configure` script:
2222
```shell
2323
$ ./configure --with-ebpf-example --with-linux=/usr/src/linux
2424
```
25+
26+
## Debug
27+
To show the maps:
28+
(`sudo` may needed)
29+
```shell
30+
$ bpftool map dump name tcp_balancing_t
31+
$ bpftool map dump name udp_balancing_t
32+
```

examples/eBPF/client_ebpf_user.c

Lines changed: 18 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,8 @@
1313
* ./client_ebpf_user 192.168.1.17 10001 2 3
1414
*/
1515

16-
#include <stdio.h>
17-
#include <stdlib.h>
18-
#include <pthread.h>
19-
#include <stdint.h>
20-
#include <string.h>
21-
#include <signal.h>
2216
#include <unistd.h>
2317
#include <arpa/inet.h>
24-
#include <netdb.h>
2518
#include <assert.h>
2619
#include <bpf/bpf.h>
2720
#include <bpf/libbpf.h>
@@ -48,77 +41,22 @@ static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va
4841
}
4942

5043
/**
51-
* Creates own custom socket
44+
* Open socket, loads eBPF program and attaches it to the opened socket.
45+
* int socketfd : socket file descriptor to listen to.
46+
* uint32_t key : index of the socket to be filled in the eBPF hash table.
47+
* uint32_t balancer_count : max values to be used in eBPF reuse. Should be <= MAX_BALANCER_COUNT.
5248
*/
53-
int create_socket(char *address, char *port)
54-
{
55-
struct addrinfo *addr_info;
56-
struct addrinfo hints;
57-
58-
memset(&hints, 0, sizeof(hints));
59-
60-
hints.ai_socktype = SOCK_DGRAM;
61-
hints.ai_family = AF_UNSPEC;
62-
hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
63-
64-
// Using getaddrinfo to support both IPv4 and IPv6
65-
int rc = getaddrinfo(address, port, &hints, &addr_info);
66-
67-
if (rc != 0) {
68-
printf("getaddrinfo error: %s\n", gai_strerror(rc));
69-
exit(EXIT_FAILURE);
70-
}
71-
72-
printf("Address type: %s | %d\n", (addr_info->ai_family == AF_INET) ? "IPv4" : "IPv6", ntohs(((struct sockaddr_in *)addr_info->ai_addr)->sin_port));
73-
74-
// create socket on UDP protocol
75-
int sockfd = socket(addr_info->ai_family, addr_info->ai_socktype, addr_info->ai_protocol);
76-
77-
// handle error
78-
if (sockfd < 0)
79-
{
80-
perror("Cannot create socket");
81-
exit(EXIT_FAILURE);
82-
}
83-
84-
// Use SO_REUSEPORT to be able to launch multiple collector on the same address
85-
int optval = 1;
86-
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(int)) < 0)
87-
{
88-
perror("Cannot set SO_REUSEPORT option on socket");
89-
exit(EXIT_FAILURE);
90-
}
91-
92-
// Setting socket buffer to default 20 MB
93-
uint64_t receive_buf_size = DEFAULT_SK_BUFF_SIZE;
94-
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &receive_buf_size, sizeof(receive_buf_size)) < 0)
95-
{
96-
perror("Cannot set buffer size");
97-
exit(EXIT_FAILURE);
98-
}
99-
100-
if (bind(sockfd, addr_info->ai_addr, (int)addr_info->ai_addrlen) == -1)
101-
{
102-
perror("Bind failed");
103-
close(sockfd);
104-
exit(EXIT_FAILURE);
105-
}
106-
107-
// free addr_info after usage
108-
freeaddrinfo(addr_info);
109-
110-
return sockfd;
111-
}
112-
113-
int open_socket_attach_ebpf(char *address, char *port, uint32_t key, uint32_t balancer_count)
49+
int attach_ebpf_to_socket(int socketfd, uint32_t key, uint32_t balancer_count)
11450
{
11551
int umap_fd, size_map_fd, prog_fd;
11652
char filename[] = BPF_KERNEL_PRG;
117-
int64_t usock;
53+
int64_t usock = socketfd;
11854
long err = 0;
11955

12056
assert(!balancer_count || key < balancer_count);
12157
assert(balancer_count <= MAX_BALANCER_COUNT);
58+
assert(usock >= 0);
59+
12260
printf("from args: Using hash bucket index %u", key);
12361
if (balancer_count > 0) printf(" (%u buckets in total)", balancer_count);
12462
puts("");
@@ -158,10 +96,6 @@ int open_socket_attach_ebpf(char *address, char *port, uint32_t key, uint32_t ba
15896
umap_fd = bpf_map__fd(udpmap);
15997
assert(umap_fd);
16098

161-
usock = create_socket(address, port);
162-
163-
assert(usock >= 0);
164-
16599
if (setsockopt(usock, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &prog_fd, sizeof(prog_fd)) != 0) {
166100
perror("Could not attach BPF prog");
167101
return 1;
@@ -200,26 +134,31 @@ int open_socket_attach_ebpf(char *address, char *port, uint32_t key, uint32_t ba
200134
return usock;
201135
}
202136

203-
204137
int main(int argc, char *argv[])
205138
{
206139
if (argc != 5)
207140
{
208141
printf("Error: arguments not valid\n");
209142
printf("Usage: ./client_ebpf_user <ip> <port> <index> <loadbalance_max>\n");
143+
printf("Example: ./client_ebpf_user 10.0.2.15 10001 0 5\n");
210144
exit(1);
211145
}
212146

213-
int sockfd = open_socket_attach_ebpf(argv[1], argv[2], atoi(argv[3]), atoi(argv[4]));
147+
printf("Listening on %s:%s\n", argv[1], argv[2]);
148+
149+
// Create a udp socket with default socket buffer
150+
int socketfd = unyte_udp_create_socket(argv[1], argv[2], DEFAULT_SK_BUFF_SIZE);
151+
152+
attach_ebpf_to_socket(socketfd, atoi(argv[3]), atoi(argv[4]));
214153

215154
// Initialize collector options
216-
unyte_udp_sk_options_t options = {0};
155+
unyte_udp_options_t options = {0};
217156
options.recvmmsg_vlen = USED_VLEN;
218-
options.socket_fd = sockfd;
157+
options.socket_fd = socketfd;
219158
printf("Listening on socket %d\n", options.socket_fd);
220159

221160
/* Initialize collector */
222-
unyte_udp_collector_t *collector = unyte_udp_start_collector_sk(&options);
161+
unyte_udp_collector_t *collector = unyte_udp_start_collector(&options);
223162
int recv_count = 0;
224163
int max = MAX_TO_RECEIVE;
225164

examples/sender_json.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <pthread.h>
21
#include <stdio.h>
32
#include <stdlib.h>
43

examples/sender_sample.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <pthread.h>
21
#include <stdio.h>
32
#include <stdlib.h>
43

0 commit comments

Comments
 (0)