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

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

Test udp_assoc_send()

This required virtualizing inet_get_src_addr() / udp_transmit_msg for the
association module

  • Property mode set to 100644
File size: 11.0 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 <io/log.h>
43#include <nettl/amap.h>
44#include <stdlib.h>
45
46#include "assoc.h"
47#include "msg.h"
48#include "pdu.h"
49#include "udp_type.h"
50
51static LIST_INITIALIZE(assoc_list);
52static FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
53static amap_t *amap;
54
55static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *);
56static errno_t udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *);
57static udp_assocs_dep_t *assocs_dep;
58
59/** Initialize associations. */
60errno_t udp_assocs_init(udp_assocs_dep_t *dep)
61{
62 errno_t rc;
63
64 rc = amap_create(&amap);
65 if (rc != EOK) {
66 assert(rc == ENOMEM);
67 return ENOMEM;
68 }
69
70 assocs_dep = dep;
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 inet_ep2_t epp;
256 errno_t rc;
257
258 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
259 assoc, remote, msg);
260
261 /* @a remote can be used to override the remote endpoint */
262 epp = assoc->ident;
263 if (remote != NULL)
264 epp.remote = *remote;
265
266 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check addr any");
267
268 if ((inet_addr_is_any(&epp.remote.addr)) ||
269 (epp.remote.port == inet_port_any))
270 return EINVAL;
271
272 /* This association has no local address set. Need to determine one. */
273 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check no local addr");
274 if (inet_addr_is_any(&epp.local.addr) && !assoc->nolocal) {
275 log_msg(LOG_DEFAULT, LVL_DEBUG, "Determine local address.");
276 rc = (*assocs_dep->get_srcaddr)(&epp.remote.addr, 0,
277 &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 - transmit");
292 rc = (*assocs_dep->transmit_msg)(&epp, msg);
293
294 if (rc != EOK)
295 return EIO;
296
297 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - success");
298 return EOK;
299}
300
301/** Get a received message.
302 *
303 * Pull one message from the association's receive queue.
304 */
305errno_t udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
306{
307 link_t *link;
308 udp_rcv_queue_entry_t *rqe;
309
310 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv()");
311
312 fibril_mutex_lock(&assoc->lock);
313 while (list_empty(&assoc->rcv_queue) && !assoc->reset) {
314 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - waiting");
315 fibril_condvar_wait(&assoc->rcv_queue_cv, &assoc->lock);
316 }
317
318 if (assoc->reset) {
319 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset");
320 fibril_mutex_unlock(&assoc->lock);
321 return ENXIO;
322 }
323
324 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - got a message");
325 link = list_first(&assoc->rcv_queue);
326 rqe = list_get_instance(link, udp_rcv_queue_entry_t, link);
327 list_remove(link);
328 fibril_mutex_unlock(&assoc->lock);
329
330 *msg = rqe->msg;
331 *remote = rqe->epp.remote;
332 free(rqe);
333
334 return EOK;
335}
336
337/** Message received.
338 *
339 * Find the association to which the message belongs and queue it.
340 */
341void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
342{
343 udp_assoc_t *assoc;
344 errno_t rc;
345
346 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", repp, msg);
347
348 assoc = udp_assoc_find_ref(repp);
349 if (assoc == NULL) {
350 log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
351 /* XXX Generate ICMP error. */
352 /* XXX Might propagate error directly by error return. */
353 udp_msg_delete(msg);
354 return;
355 }
356
357 if (0) {
358 rc = udp_assoc_queue_msg(assoc, repp, msg);
359 if (rc != EOK) {
360 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
361 /* XXX Generate ICMP error? */
362 }
363 }
364
365 log_msg(LOG_DEFAULT, LVL_DEBUG, "call assoc->cb->recv_msg");
366 assoc->cb->recv_msg(assoc->cb_arg, repp, msg);
367 udp_assoc_delref(assoc);
368}
369
370/** Reset association.
371 *
372 * This causes any pendingreceive operations to return immediately with
373 * UDP_ERESET.
374 */
375void udp_assoc_reset(udp_assoc_t *assoc)
376{
377 fibril_mutex_lock(&assoc->lock);
378 assoc->reset = true;
379 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
380 fibril_mutex_unlock(&assoc->lock);
381}
382
383static errno_t udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
384 udp_msg_t *msg)
385{
386 udp_rcv_queue_entry_t *rqe;
387
388 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
389 assoc, epp, msg);
390
391 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
392 if (rqe == NULL)
393 return ENOMEM;
394
395 link_initialize(&rqe->link);
396 rqe->epp = *epp;
397 rqe->msg = msg;
398
399 fibril_mutex_lock(&assoc->lock);
400 list_append(&rqe->link, &assoc->rcv_queue);
401 fibril_mutex_unlock(&assoc->lock);
402
403 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
404
405 return EOK;
406}
407
408/** Find association structure for specified endpoint pair.
409 *
410 * An association is uniquely identified by an endpoint pair. Look up our
411 * association map and return association structure based on endpoint pair.
412 * The association reference count is bumped by one.
413 *
414 * @param epp Endpoint pair
415 * @return Association structure or NULL if not found.
416 */
417static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp)
418{
419 errno_t rc;
420 void *arg;
421 udp_assoc_t *assoc;
422
423 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", epp);
424 fibril_mutex_lock(&assoc_list_lock);
425
426 rc = amap_find_match(amap, epp, &arg);
427 if (rc != EOK) {
428 assert(rc == ENOENT);
429 fibril_mutex_unlock(&assoc_list_lock);
430 return NULL;
431 }
432
433 assoc = (udp_assoc_t *)arg;
434 udp_assoc_addref(assoc);
435
436 fibril_mutex_unlock(&assoc_list_lock);
437 return assoc;
438}
439
440/**
441 * @}
442 */
Note: See TracBrowser for help on using the repository browser.