1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "mpu.h"
#include "heap.h"
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <time.h>
int mpu_init(mpu_ctx_t* ctx,
int sock0, float weight0,
int sock1, float weight1,
int reorder_window,
size_t reorder_slots
) {
if (!ctx || sock0 < 0 || sock1 < 0
|| weight0 <= 0.0F || weight1 <= 0.0F
|| reorder_window <= 0
|| reorder_slots == 0) {
errno = EINVAL;
return -1;
}
ctx->paths[0] = (mpu_path_t) {
.sockfd = sock0,
.weight = weight0
};
ctx->paths[1] = (mpu_path_t) {
.sockfd = sock1,
.weight = weight1
};
ctx->credits[0] = 0.0F;
ctx->credits[1] = 0.0F;
ctx->send_seq_num = 1;
ctx->recv_seq_num = 1;
ctx->reorder_window = reorder_window;
int i = heap_init(&ctx->reorder_heap, reorder_slots, sizeof(mpu_slot_t));
if (i < 0) {
return -1;
}
return 0;
}
// Helper to get current time in milliseconds
static inline uint64_t get_time_now(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t) ts.tv_sec * 1000ULL
+ ((uint64_t) ts.tv_nsec / 1000000ULL);
}
// FIXME: seq_num is never converted to network byte order
// TODO: Zero-copy send() requiring the user to reserve space for the header.
// Perhaps consider MSG_MORE for sending header & payload separately or sendmsg()
int mpu_send(mpu_ctx_t* ctx, const uint8_t* data, size_t len) {
if (!ctx || !data || len == 0 || len > MPU_MTU - sizeof(mpu_hdr_t)) {
errno = EINVAL;
return -1;
}
// Prepare packet with header
size_t packet_len = sizeof(mpu_hdr_t) + len;
union {
uint8_t buf[MPU_MTU];
mpu_hdr_t hdr;
} packet;
memcpy(packet.buf + sizeof(mpu_hdr_t), data, len);
packet.hdr = (mpu_hdr_t) {
.seq_num = ctx->send_seq_num++
};
// Select path using weighted round-robin scheduler
int path_idx = (ctx->credits[0] <= ctx->credits[1]) ? 0 : 1;
ctx->credits[path_idx] += (float) len / ctx->paths[path_idx].weight;
ssize_t sent = send(ctx->paths[path_idx].sockfd, packet.buf, packet_len, 0);
if (sent < 0) {
return -1;
}
return 0;
}
// FIXME: Skipped packages are never purged
// TODO: Zero-copy recv() into reorder buffer slot, requiring the user to provide a callback
// for processing received packets, instead of copying into a user buffer.
// TODO: Attempt to optimize heap_extract() to purge multiple expired packets at once.
ssize_t mpu_recv(mpu_ctx_t* ctx, uint8_t* data, size_t len) {
if (!ctx || !data || !len) {
errno = EINVAL;
return -1;
}
while (true) {
// Peek heap for next in-order packet and deliver if available
heap_node_t* node = heap_next_extract(&ctx->reorder_heap);
if (node && node->value == ctx->recv_seq_num) {
mpu_slot_t* slot = (mpu_slot_t*) node->user;
size_t data_len = slot->packet_len - sizeof(mpu_hdr_t);
if (data_len > len) {
data_len = len;
}
memcpy(data, slot->packet.buf + sizeof(mpu_hdr_t), data_len);
heap_extract(&ctx->reorder_heap);
ctx->recv_seq_num++;
return (ssize_t) data_len;
}
// Purge packets outside of reorder window from heap
uint64_t now = get_time_now();
int expires_in = ctx->reorder_window;
while (true) {
node = heap_next_extract(&ctx->reorder_heap);
if (!node) {
break;
}
mpu_slot_t* slot = (mpu_slot_t*) node->user;
int age = (int) (now - slot->timestamp);
if (age < ctx->reorder_window) {
expires_in = ctx->reorder_window - age;
break;
}
ctx->recv_seq_num = slot->packet.hdr.seq_num + 1;
heap_extract(&ctx->reorder_heap);
}
// Receive from either path
struct pollfd pfds[2] = {
{ .fd = ctx->paths[0].sockfd, .events = POLLIN },
{ .fd = ctx->paths[1].sockfd, .events = POLLIN }
};
int ret = poll(pfds, 2, expires_in);
if (ret < 0) {
if (errno == EINTR) {
continue;
}
return -1;
} else if (ret == 0) {
continue;
}
for (int i = 0; i < 2; i++) {
if (!(pfds[i].revents & POLLIN)) {
continue;
}
// Write to reorder buffer
node = heap_next_insert(&ctx->reorder_heap);
if (!node) {
// No free slot, skip packet for now
continue;
}
mpu_slot_t* slot = (mpu_slot_t*) node->user;
ssize_t packet_len = recv(pfds[i].fd, slot->packet.buf, MPU_MTU, 0);
if (packet_len < 0) {
if (errno == EINTR) {
continue;
}
return -1;
} else if (packet_len < (ssize_t) sizeof(mpu_hdr_t)) {
// Malformed packet, ignore
continue;
}
slot->timestamp = get_time_now();
slot->packet_len = (uint16_t) packet_len;
heap_insert(&ctx->reorder_heap, slot->packet.hdr.seq_num);
}
}
}
|