forked from PDXostc/reliable_multicast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmc_pub_write.c
384 lines (301 loc) · 11.1 KB
/
rmc_pub_write.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright (C) 2018, Jaguar Land Rover
// This program is licensed under the terms and conditions of the
// Mozilla Public License, version 2.0. The full text of the
// Mozilla Public License is at https://www.mozilla.org/MPL/2.0/
//
// Author: Magnus Feuer ([email protected])
#define _GNU_SOURCE
#include "reliable_multicast.h"
#include "rmc_log.h"
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <assert.h>
static void setup_packet_header(rmc_pub_context_t* ctx,
pub_packet_t* pack,
packet_header_t* pack_hdr)
{
// Setup node id.
pack_hdr->node_id = ctx->node_id;
pack_hdr->payload_len = pack->payload_len;
pack_hdr->pid = pack->pid;
// If listen_ip == 0 then receiver will use source address of packet as tcp
// address to connect to.
pack_hdr->listen_ip = ctx->control_listen_if_addr;
pack_hdr->listen_port = ctx->control_listen_port;
}
// FIXME: If we see timed out (== lost) packets from subscribers, we should
// switch them to TCP for a period of time in order
// to use TCP's flow control until congestion has eased.
static int send_single_multicast_packet(rmc_pub_context_t* ctx, pub_packet_t* pack)
{
pub_context_t* pctx = &ctx->pub_ctx;
uint8_t packet[RMC_MAX_PACKET];
uint8_t *packet_ptr = packet;
packet_id_t pid = 0;
usec_timestamp_t ts = 0;
ssize_t res = 0;
packet_header_t pack_hdr;
struct msghdr msg_hdr;
struct iovec io_vec[2];
struct sockaddr_in sock_addr = (struct sockaddr_in) {
.sin_family = AF_INET,
.sin_port = htons(ctx->mcast_port),
.sin_addr = (struct in_addr) { .s_addr = htonl(ctx->mcast_group_addr) }
};
if (ctx->mcast_send_descriptor == -1)
return ENOTCONN;
// Will the packet fit inside a UDP packet?
if (sizeof(packet_header_t) + pack->payload_len > RMC_MAX_PACKET)
return EMSGSIZE;
setup_packet_header(ctx, pack, &pack_hdr);
// char listen_addr[128];
// char mcast_addr[128];
// strcpy(listen_addr, inet_ntoa( (struct in_addr) { .s_addr = htonl(ctx->control_listen_if_addr) }));
// strcpy(mcast_addr, inet_ntoa( (struct in_addr) { .s_addr = htonl(ctx->mcast_group_addr) }));
// printf("mcast_tx(): mcast[%s:%d] listen[%s:%d] pid[%lu:%lu] - len[%.5lu]\n",
// mcast_addr, ntohs(sock_addr.sin_port),
// listen_addr, pack_hdr->listen_port,
// first_sent, last_sent, sizeof(packet_header_t) + pack_hdr->payload_len);
// Setup the scattered-write io vector with header and payload
io_vec[0] = (struct iovec) {
.iov_base = (void*) &pack_hdr,
.iov_len = sizeof(pack_hdr)
};
io_vec[1] = (struct iovec) {
.iov_base = (void*) pack->payload,
.iov_len = pack->payload_len
};
// Setup message header.
msg_hdr = (struct msghdr) {
.msg_name = (void*) &sock_addr,
.msg_namelen = sizeof(sock_addr),
.msg_iov = io_vec,
.msg_iovlen = sizeof(io_vec) / sizeof(io_vec[0]),
.msg_control = 0,
.msg_controllen = 0,
.msg_flags = 0
};
res = sendmsg(ctx->mcast_send_descriptor, &msg_hdr, MSG_DONTWAIT);
if (res == -1) {
if ( errno != EAGAIN && errno != EWOULDBLOCK) {
RMC_LOG_INDEX_WARNING(RMC_MULTICAST_INDEX,
"sendmsg(): %d/%s", errno, strerror(errno));
return errno;
}
// We are ok with an EAGAIN error since we can retry later to
// send the packet.
res = EAGAIN;
goto rearm;
}
pub_packet_sent(pctx, pack, rmc_usec_monotonic_timestamp());
res = 0;
rearm:
if (ctx->conn_vec.poll_modify) {
// Do we have more packets to send?
(*ctx->conn_vec.poll_modify)(ctx->user_data,
ctx->mcast_send_descriptor,
RMC_MULTICAST_INDEX,
RMC_POLLWRITE,
pub_next_queued_packet(pctx)?RMC_POLLWRITE:0);
}
return res;
}
static int process_multicast_write(rmc_pub_context_t* ctx)
{
pub_context_t* pctx = &ctx->pub_ctx;
pub_packet_t* pack = pub_next_queued_packet(pctx);
int res = 0;
int count = 0;
while(pack) {
if ((res = send_single_multicast_packet(ctx, pack)))
break;
++count;
pack = pub_next_queued_packet(pctx);
}
return res;
}
int rmc_pub_resend_packet(rmc_pub_context_t* ctx,
rmc_connection_t* conn,
pub_packet_t* pack)
{
uint8_t *seg1 = 0;
uint32_t seg1_len = 0;
uint8_t *seg2 = 0;
uint32_t seg2_len = 0;
int res = 0;
packet_header_t pack_hdr;
// Do we have enough circular buffer meomory available?
if (circ_buf_available(&conn->write_buf) < 1 + sizeof(packet_header_t) + pack->payload_len) {
RMC_LOG_INDEX_DEBUG(conn->connection_index,
"Resend packet needed %d+%d+%d=%d bytes, got %d",
1, sizeof(packet_header_t),pack->payload_len,
1 + sizeof(packet_header_t) + pack->payload_len,
circ_buf_available(&conn->write_buf));
res = EAGAIN;
goto rearm;
}
// Allocate memory for command
res = circ_buf_alloc(&conn->write_buf, 1,
&seg1, &seg1_len,
&seg2, &seg2_len);
// We checked above that we have memory, so an error here is final
if (res) {
RMC_LOG_INDEX_FATAL(conn->connection_index,
"Could not allocate one byte: %s\n", strerror(errno));
exit(255);
}
*seg1 = RMC_CMD_PACKET;
setup_packet_header(ctx, pack, &pack_hdr);
// Allocate memory for packet header
res = circ_buf_alloc(&conn->write_buf, sizeof(packet_header_t),
&seg1, &seg1_len,
&seg2, &seg2_len);
if (res) {
RMC_LOG_INDEX_FATAL(conn->connection_index,
"Could not allocate %lu header bytes: %s\n", sizeof(pack_hdr), strerror(errno));
exit(255);
}
// Copy in packet header
memcpy(seg1, (uint8_t*) &pack_hdr, seg1_len);
if (seg2_len)
memcpy(seg2, ((uint8_t*) &pack_hdr) + seg1_len, seg2_len);
// Allocate packet payload
res = circ_buf_alloc(&conn->write_buf, pack->payload_len,
&seg1, &seg1_len,
&seg2, &seg2_len);
if (res) {
RMC_LOG_INDEX_FATAL(conn->connection_index,
"Could not allocate %d payload bytes: %s\n", pack->payload_len, strerror(errno));
exit(255);
}
// Copy in packet payload
memcpy(seg1, pack->payload, seg1_len);
if (seg2_len)
memcpy(seg2, ((uint8_t*) pack->payload) + seg1_len, seg2_len);
RMC_LOG_INDEX_DEBUG(conn->connection_index,
"Queued %d bytes",
1 + sizeof(packet_header_t) + pack->payload_len);
rearm:
// Setup the poll write action
if (!(conn->action & RMC_POLLWRITE)) {
rmc_poll_action_t old_action = conn->action;
conn->action |= RMC_POLLWRITE;
if (ctx->conn_vec.poll_modify)
(*ctx->conn_vec.poll_modify)(ctx->user_data,
conn->descriptor,
conn->connection_index,
old_action,
conn->action);
}
return res;
}
int rmc_pub_write(rmc_pub_context_t* ctx, rmc_index_t s_ind, uint8_t* op_res)
{
int res = 0;
int rearm_write = 0;
uint32_t bytes_left_before = 0;
uint32_t bytes_left_after = 0;
rmc_poll_action_t old_action = 0;
rmc_connection_t* conn = 0;
assert(ctx);
if (s_ind == RMC_MULTICAST_INDEX) {
if (op_res)
*op_res = RMC_WRITE_MULTICAST;
// Write as many multicast packets as we can.
RMC_LOG_INDEX_DEBUG(RMC_MULTICAST_INDEX,
"Processing multicast write");
return process_multicast_write(ctx);
}
// Is s_ind within our connection vector?
if (s_ind >= rmc_pub_get_max_subscriber_count(ctx)) {
if (op_res)
*op_res = RMC_ERROR;
return EINVAL;
}
conn = rmc_conn_find_by_index(&ctx->conn_vec, s_ind);
if (!conn || conn->mode != RMC_CONNECTION_MODE_CONNECTED) {
if (op_res)
*op_res = RMC_ERROR;
return ENOTCONN;
}
old_action = conn->action;
// Do we have any data to write?
if (circ_buf_in_use(&conn->write_buf) == 0) {
if (op_res)
*op_res = RMC_ERROR;
return ENODATA;
}
if (op_res)
*op_res = RMC_WRITE_TCP;
res = rmc_conn_process_tcp_write(conn, &bytes_left_after);
if (bytes_left_after == 0)
conn->action &= ~RMC_POLLWRITE;
else
conn->action |= RMC_POLLWRITE;
if (ctx->conn_vec.poll_modify)
(*ctx->conn_vec.poll_modify)(ctx->user_data,
conn->descriptor,
s_ind,
old_action,
conn->action);
// Did we encounter an error.
if (res && op_res)
*op_res = RMC_ERROR;
return res;
}
// FIXME MEDIUM: Not the fastest. But this function
// *should* mostly be used during shutdown.
int rmc_pub_context_get_pending(rmc_pub_context_t* ctx,
uint32_t* queued_packets,
uint32_t* send_buf_len,
uint32_t* ack_count)
{
payload_len_t len = 0;
rmc_index_t ind = 0;
int count = 0;
rmc_index_t max_ind = 0;
uint8_t busy = 0;
uint32_t queue_size = 0;
if (!ctx)
EINVAL;
if (ack_count)
*ack_count = 0;
if (send_buf_len)
*send_buf_len = 0;
queue_size = pub_queue_size(&ctx->pub_ctx);
if (queue_size > 0)
busy = 1;
if (queued_packets)
*queued_packets = queue_size;
rmc_conn_get_max_index_in_use(&ctx->conn_vec, &max_ind);
// If we have no subscribers, just return immediately
if (max_ind == -1)
// Return EBUSY if we have pending data to transmit
return busy?EBUSY:0;
for(ind = 0; ind <= max_ind; ++ind) {
rmc_connection_t* conn = 0;
pub_subscriber_t *sub = 0;
payload_len_t len = 0;
conn = rmc_conn_find_by_index(&ctx->conn_vec, ind);
if (!conn)
continue;
sub = &ctx->subscribers[conn->connection_index];
count = pub_packet_list_size(&sub->inflight);
if (count != 0) {
busy = 1;
if (ack_count)
*ack_count += count;
}
rmc_conn_get_pending_send_length(conn, &len);
if (len != 0) {
busy = 1;
if (send_buf_len)
*send_buf_len += len;
}
}
// Return EBUSY if we have pending data to transmit
return busy?EBUSY:0;
}