source: mainline/uspace/srv/net/udp/assoc.c@ 25525133

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 25525133 was 16b0ac3, checked in by Jiri Svoboda <jiri@…>, 6 years ago

Start adding unit tests for UDP associations

  • Property mode set to 100644
File size: 11.1 KB
Line 
1/*
2 * Copyright (c) 2015 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup udp
30 * @{
31 */
32
33/**
34 * @file UDP associations
35 */
36
37#include <adt/list.h>
38#include <errno.h>
39#include <stdbool.h>
40#include <fibril_synch.h>
41#include <inet/endpoint.h>
42#include <inet/inet.h>
43#include <io/log.h>
44#include <nettl/amap.h>
45#include <stdlib.h>
46
47#include "assoc.h"
48#include "msg.h"
49#include "pdu.h"
50#include "udp_inet.h"
51#include "udp_type.h"
52
53static LIST_INITIALIZE(assoc_list);
54static FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
55static amap_t *amap;
56
57static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *);
58static errno_t udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *);
59
60/** Initialize associations. */
61errno_t udp_assocs_init(void)
62{
63 errno_t rc;
64
65 rc = amap_create(&amap);
66 if (rc != EOK) {
67 assert(rc == ENOMEM);
68 return ENOMEM;
69 }
70
71 return EOK;
72}
73
74/** Finalize associations. */
75void udp_assocs_fini(void)
76{
77 assert(list_empty(&assoc_list));
78
79 amap_destroy(amap);
80 amap = NULL;
81}
82
83/** Create new association structure.
84 *
85 * @param epp Endpoint pair (will be copied)
86 * @param cb Callbacks
87 * @param cb_arg Callback argument
88 * @return New association or NULL
89 */
90udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg)
91{
92 udp_assoc_t *assoc = NULL;
93
94 /* Allocate association structure */
95 assoc = calloc(1, sizeof(udp_assoc_t));
96 if (assoc == NULL)
97 goto error;
98
99 fibril_mutex_initialize(&assoc->lock);
100
101 /* One for the user */
102 refcount_init(&assoc->refcnt);
103
104 /* Initialize receive queue */
105 list_initialize(&assoc->rcv_queue);
106 fibril_condvar_initialize(&assoc->rcv_queue_cv);
107
108 if (epp != NULL)
109 assoc->ident = *epp;
110
111 assoc->cb = cb;
112 assoc->cb_arg = cb_arg;
113 return assoc;
114error:
115 return NULL;
116}
117
118/** Destroy association structure.
119 *
120 * Association structure should be destroyed when the folowing conditions
121 * are met:
122 * (1) user has deleted the association
123 * (2) nobody is holding references to the association
124 *
125 * This happens when @a assoc->refcnt is zero as we count (1)
126 * as an extra reference.
127 *
128 * @param assoc Association
129 */
130static void udp_assoc_free(udp_assoc_t *assoc)
131{
132 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_free(%p)", assoc->name, assoc);
133
134 while (!list_empty(&assoc->rcv_queue)) {
135 link_t *link = list_first(&assoc->rcv_queue);
136 udp_rcv_queue_entry_t *rqe = list_get_instance(link,
137 udp_rcv_queue_entry_t, link);
138 list_remove(link);
139
140 udp_msg_delete(rqe->msg);
141 free(rqe);
142 }
143
144 free(assoc);
145}
146
147/** Add reference to association.
148 *
149 * Increase association reference count by one.
150 *
151 * @param assoc Association
152 */
153void udp_assoc_addref(udp_assoc_t *assoc)
154{
155 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: upd_assoc_addref(%p)", assoc->name, assoc);
156 refcount_up(&assoc->refcnt);
157}
158
159/** Remove reference from association.
160 *
161 * Decrease association reference count by one.
162 *
163 * @param assoc Association
164 */
165void udp_assoc_delref(udp_assoc_t *assoc)
166{
167 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delref(%p)", assoc->name, assoc);
168
169 if (refcount_down(&assoc->refcnt))
170 udp_assoc_free(assoc);
171}
172
173/** Delete association.
174 *
175 * The caller promises not make no further references to @a assoc.
176 * UDP will free @a assoc eventually.
177 *
178 * @param assoc Association
179 */
180void udp_assoc_delete(udp_assoc_t *assoc)
181{
182 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delete(%p)", assoc->name, assoc);
183
184 assert(assoc->deleted == false);
185 assoc->deleted = true;
186 udp_assoc_delref(assoc);
187}
188
189/** Enlist association.
190 *
191 * Add association to the association map.
192 */
193errno_t udp_assoc_add(udp_assoc_t *assoc)
194{
195 inet_ep2_t aepp;
196 errno_t rc;
197
198 udp_assoc_addref(assoc);
199 fibril_mutex_lock(&assoc_list_lock);
200
201 rc = amap_insert(amap, &assoc->ident, assoc, af_allow_system, &aepp);
202 if (rc != EOK) {
203 udp_assoc_delref(assoc);
204 fibril_mutex_unlock(&assoc_list_lock);
205 return rc;
206 }
207
208 assoc->ident = aepp;
209 list_append(&assoc->link, &assoc_list);
210 fibril_mutex_unlock(&assoc_list_lock);
211
212 return EOK;
213}
214
215/** Delist association.
216 *
217 * Remove association from the association map.
218 */
219void udp_assoc_remove(udp_assoc_t *assoc)
220{
221 fibril_mutex_lock(&assoc_list_lock);
222 amap_remove(amap, &assoc->ident);
223 list_remove(&assoc->link);
224 fibril_mutex_unlock(&assoc_list_lock);
225 udp_assoc_delref(assoc);
226}
227
228/** Set IP link in association.
229 *
230 * @param assoc Association
231 * @param iplink IP link
232 */
233void udp_assoc_set_iplink(udp_assoc_t *assoc, service_id_t iplink)
234{
235 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_iplink(%p, %zu)",
236 assoc, iplink);
237 fibril_mutex_lock(&assoc->lock);
238 assoc->ident.local_link = iplink;
239 fibril_mutex_unlock(&assoc->lock);
240}
241
242/** Send message to association.
243 *
244 * @param assoc Association
245 * @param remote Remote endpoint or NULL not to override @a assoc
246 * @param msg Message
247 *
248 * @return EOK on success
249 * EINVAL if remote endpoint is not set
250 * ENOMEM if out of resources
251 * EIO if no route to destination exists
252 */
253errno_t udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg)
254{
255 udp_pdu_t *pdu;
256 inet_ep2_t epp;
257 errno_t rc;
258
259 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
260 assoc, remote, msg);
261
262 /* @a remote can be used to override the remote endpoint */
263 epp = assoc->ident;
264 if (remote != NULL)
265 epp.remote = *remote;
266
267 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check addr any");
268
269 if ((inet_addr_is_any(&epp.remote.addr)) ||
270 (epp.remote.port == inet_port_any))
271 return EINVAL;
272
273 /* This association has no local address set. Need to determine one. */
274 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check no local addr");
275 if (inet_addr_is_any(&epp.local.addr) && !assoc->nolocal) {
276 log_msg(LOG_DEFAULT, LVL_DEBUG, "Determine local address.");
277 rc = inet_get_srcaddr(&epp.remote.addr, 0, &epp.local.addr);
278 if (rc != EOK) {
279 log_msg(LOG_DEFAULT, LVL_DEBUG, "Cannot determine "
280 "local address.");
281 return EINVAL;
282 }
283 }
284
285 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check version");
286
287 if (!inet_addr_is_any(&epp.local.addr) &&
288 epp.remote.addr.version != epp.local.addr.version)
289 return EINVAL;
290
291 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - encode pdu");
292
293 rc = udp_pdu_encode(&epp, msg, &pdu);
294 if (rc != EOK)
295 return ENOMEM;
296
297 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - transmit");
298
299 rc = udp_transmit_pdu(pdu);
300 udp_pdu_delete(pdu);
301
302 if (rc != EOK)
303 return EIO;
304
305 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - success");
306 return EOK;
307}
308
309/** Get a received message.
310 *
311 * Pull one message from the association's receive queue.
312 */
313errno_t udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
314{
315 link_t *link;
316 udp_rcv_queue_entry_t *rqe;
317
318 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv()");
319
320 fibril_mutex_lock(&assoc->lock);
321 while (list_empty(&assoc->rcv_queue) && !assoc->reset) {
322 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - waiting");
323 fibril_condvar_wait(&assoc->rcv_queue_cv, &assoc->lock);
324 }
325
326 if (assoc->reset) {
327 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset");
328 fibril_mutex_unlock(&assoc->lock);
329 return ENXIO;
330 }
331
332 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - got a message");
333 link = list_first(&assoc->rcv_queue);
334 rqe = list_get_instance(link, udp_rcv_queue_entry_t, link);
335 list_remove(link);
336 fibril_mutex_unlock(&assoc->lock);
337
338 *msg = rqe->msg;
339 *remote = rqe->epp.remote;
340 free(rqe);
341
342 return EOK;
343}
344
345/** Message received.
346 *
347 * Find the association to which the message belongs and queue it.
348 */
349void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
350{
351 udp_assoc_t *assoc;
352 errno_t rc;
353
354 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", repp, msg);
355
356 assoc = udp_assoc_find_ref(repp);
357 if (assoc == NULL) {
358 log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
359 /* XXX Generate ICMP error. */
360 /* XXX Might propagate error directly by error return. */
361 udp_msg_delete(msg);
362 return;
363 }
364
365 if (0) {
366 rc = udp_assoc_queue_msg(assoc, repp, msg);
367 if (rc != EOK) {
368 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
369 /* XXX Generate ICMP error? */
370 }
371 }
372
373 log_msg(LOG_DEFAULT, LVL_DEBUG, "call assoc->cb->recv_msg");
374 assoc->cb->recv_msg(assoc->cb_arg, repp, msg);
375 udp_assoc_delref(assoc);
376}
377
378/** Reset association.
379 *
380 * This causes any pendingreceive operations to return immediately with
381 * UDP_ERESET.
382 */
383void udp_assoc_reset(udp_assoc_t *assoc)
384{
385 fibril_mutex_lock(&assoc->lock);
386 assoc->reset = true;
387 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
388 fibril_mutex_unlock(&assoc->lock);
389}
390
391static errno_t udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
392 udp_msg_t *msg)
393{
394 udp_rcv_queue_entry_t *rqe;
395
396 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
397 assoc, epp, msg);
398
399 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
400 if (rqe == NULL)
401 return ENOMEM;
402
403 link_initialize(&rqe->link);
404 rqe->epp = *epp;
405 rqe->msg = msg;
406
407 fibril_mutex_lock(&assoc->lock);
408 list_append(&rqe->link, &assoc->rcv_queue);
409 fibril_mutex_unlock(&assoc->lock);
410
411 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
412
413 return EOK;
414}
415
416/** Find association structure for specified endpoint pair.
417 *
418 * An association is uniquely identified by an endpoint pair. Look up our
419 * association map and return association structure based on endpoint pair.
420 * The association reference count is bumped by one.
421 *
422 * @param epp Endpoint pair
423 * @return Association structure or NULL if not found.
424 */
425static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp)
426{
427 errno_t rc;
428 void *arg;
429 udp_assoc_t *assoc;
430
431 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", epp);
432 fibril_mutex_lock(&assoc_list_lock);
433
434 rc = amap_find_match(amap, epp, &arg);
435 if (rc != EOK) {
436 assert(rc == ENOENT);
437 fibril_mutex_unlock(&assoc_list_lock);
438 return NULL;
439 }
440
441 assoc = (udp_assoc_t *)arg;
442 udp_assoc_addref(assoc);
443
444 fibril_mutex_unlock(&assoc_list_lock);
445 return assoc;
446}
447
448/**
449 * @}
450 */
Note: See TracBrowser for help on using the repository browser.