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
RevLine 
[ee603c4]1/*
[2f19103]2 * Copyright (c) 2015 Jiri Svoboda
[ee603c4]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>
[fab2746]38#include <errno.h>
[3e6a98c5]39#include <stdbool.h>
[ee603c4]40#include <fibril_synch.h>
[58e9dec]41#include <inet/endpoint.h>
[ee603c4]42#include <io/log.h>
[2989c7e]43#include <nettl/amap.h>
[ee603c4]44#include <stdlib.h>
45
46#include "assoc.h"
47#include "msg.h"
48#include "pdu.h"
49#include "udp_type.h"
50
[2989c7e]51static LIST_INITIALIZE(assoc_list);
52static FIBRIL_MUTEX_INITIALIZE(assoc_list_lock);
53static amap_t *amap;
[ee603c4]54
[2f19103]55static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *);
[b7fd2a0]56static errno_t udp_assoc_queue_msg(udp_assoc_t *, inet_ep2_t *, udp_msg_t *);
[89ba88c]57static udp_assocs_dep_t *assocs_dep;
[92b42442]58
[2989c7e]59/** Initialize associations. */
[89ba88c]60errno_t udp_assocs_init(udp_assocs_dep_t *dep)
[2989c7e]61{
[b7fd2a0]62 errno_t rc;
[2989c7e]63
64 rc = amap_create(&amap);
65 if (rc != EOK) {
66 assert(rc == ENOMEM);
67 return ENOMEM;
68 }
69
[89ba88c]70 assocs_dep = dep;
[2989c7e]71 return EOK;
72}
73
[16b0ac3]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
[ee603c4]83/** Create new association structure.
84 *
[2f19103]85 * @param epp Endpoint pair (will be copied)
86 * @param cb Callbacks
87 * @param cb_arg Callback argument
[ee603c4]88 * @return New association or NULL
89 */
[2f19103]90udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg)
[ee603c4]91{
92 udp_assoc_t *assoc = NULL;
93
[92b42442]94 /* Allocate association structure */
[ee603c4]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 */
[498ced1]102 refcount_init(&assoc->refcnt);
[ee603c4]103
104 /* Initialize receive queue */
105 list_initialize(&assoc->rcv_queue);
106 fibril_condvar_initialize(&assoc->rcv_queue_cv);
107
[2f19103]108 if (epp != NULL)
109 assoc->ident = *epp;
[ee603c4]110
[fab2746]111 assoc->cb = cb;
112 assoc->cb_arg = cb_arg;
[ee603c4]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 *
[92b42442]128 * @param assoc Association
[ee603c4]129 */
130static void udp_assoc_free(udp_assoc_t *assoc)
131{
[a1a101d]132 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_free(%p)", assoc->name, assoc);
[ee603c4]133
134 while (!list_empty(&assoc->rcv_queue)) {
135 link_t *link = list_first(&assoc->rcv_queue);
[92b42442]136 udp_rcv_queue_entry_t *rqe = list_get_instance(link,
137 udp_rcv_queue_entry_t, link);
[ee603c4]138 list_remove(link);
[92b42442]139
140 udp_msg_delete(rqe->msg);
141 free(rqe);
[ee603c4]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{
[a1a101d]155 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: upd_assoc_addref(%p)", assoc->name, assoc);
[498ced1]156 refcount_up(&assoc->refcnt);
[ee603c4]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{
[a1a101d]167 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delref(%p)", assoc->name, assoc);
[ee603c4]168
[498ced1]169 if (refcount_down(&assoc->refcnt))
[ee603c4]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{
[a1a101d]182 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delete(%p)", assoc->name, assoc);
[ee603c4]183
184 assert(assoc->deleted == false);
185 assoc->deleted = true;
[16b0ac3]186 udp_assoc_delref(assoc);
[ee603c4]187}
188
189/** Enlist association.
190 *
191 * Add association to the association map.
192 */
[b7fd2a0]193errno_t udp_assoc_add(udp_assoc_t *assoc)
[ee603c4]194{
[2989c7e]195 inet_ep2_t aepp;
[b7fd2a0]196 errno_t rc;
[2989c7e]197
[ee603c4]198 udp_assoc_addref(assoc);
199 fibril_mutex_lock(&assoc_list_lock);
[2989c7e]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;
[ee603c4]209 list_append(&assoc->link, &assoc_list);
210 fibril_mutex_unlock(&assoc_list_lock);
[2989c7e]211
212 return EOK;
[ee603c4]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);
[2989c7e]222 amap_remove(amap, &assoc->ident);
[ee603c4]223 list_remove(&assoc->link);
224 fibril_mutex_unlock(&assoc_list_lock);
225 udp_assoc_delref(assoc);
226}
227
[695b6ff]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);
[2f19103]238 assoc->ident.local_link = iplink;
[695b6ff]239 fibril_mutex_unlock(&assoc->lock);
240}
241
[ee603c4]242/** Send message to association.
243 *
244 * @param assoc Association
[2f19103]245 * @param remote Remote endpoint or NULL not to override @a assoc
[ee603c4]246 * @param msg Message
247 *
248 * @return EOK on success
[2f19103]249 * EINVAL if remote endpoint is not set
[ee603c4]250 * ENOMEM if out of resources
251 * EIO if no route to destination exists
252 */
[b7fd2a0]253errno_t udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg)
[ee603c4]254{
[2f19103]255 inet_ep2_t epp;
[b7fd2a0]256 errno_t rc;
[ee603c4]257
[c3f7d37]258 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
[2f19103]259 assoc, remote, msg);
[92b42442]260
[2f19103]261 /* @a remote can be used to override the remote endpoint */
262 epp = assoc->ident;
263 if (remote != NULL)
264 epp.remote = *remote;
[ee603c4]265
[c3f7d37]266 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check addr any");
[fab2746]267
[2f19103]268 if ((inet_addr_is_any(&epp.remote.addr)) ||
[58e9dec]269 (epp.remote.port == inet_port_any))
[ee603c4]270 return EINVAL;
271
[58e8646]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.");
[89ba88c]276 rc = (*assocs_dep->get_srcaddr)(&epp.remote.addr, 0,
277 &epp.local.addr);
[58e8646]278 if (rc != EOK) {
279 log_msg(LOG_DEFAULT, LVL_DEBUG, "Cannot determine "
280 "local address.");
281 return EINVAL;
282 }
283 }
284
[c3f7d37]285 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check version");
[fab2746]286
[58e8646]287 if (!inet_addr_is_any(&epp.local.addr) &&
288 epp.remote.addr.version != epp.local.addr.version)
[fab2746]289 return EINVAL;
290
[c3f7d37]291 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - transmit");
[89ba88c]292 rc = (*assocs_dep->transmit_msg)(&epp, msg);
[071a2c60]293
[ee603c4]294 if (rc != EOK)
295 return EIO;
296
[c3f7d37]297 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - success");
[ee603c4]298 return EOK;
299}
300
[92b42442]301/** Get a received message.
302 *
303 * Pull one message from the association's receive queue.
304 */
[b7fd2a0]305errno_t udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
[92b42442]306{
307 link_t *link;
308 udp_rcv_queue_entry_t *rqe;
309
[c3f7d37]310 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv()");
[92b42442]311
312 fibril_mutex_lock(&assoc->lock);
[141a20d]313 while (list_empty(&assoc->rcv_queue) && !assoc->reset) {
[a1a101d]314 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - waiting");
[92b42442]315 fibril_condvar_wait(&assoc->rcv_queue_cv, &assoc->lock);
316 }
317
[141a20d]318 if (assoc->reset) {
[a1e2df13]319 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset");
[141a20d]320 fibril_mutex_unlock(&assoc->lock);
[fab2746]321 return ENXIO;
[141a20d]322 }
323
[c3f7d37]324 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - got a message");
[92b42442]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;
[2f19103]331 *remote = rqe->epp.remote;
[7094e196]332 free(rqe);
[92b42442]333
334 return EOK;
335}
336
337/** Message received.
338 *
339 * Find the association to which the message belongs and queue it.
340 */
[2f19103]341void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
[92b42442]342{
343 udp_assoc_t *assoc;
[b7fd2a0]344 errno_t rc;
[92b42442]345
[c3f7d37]346 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", repp, msg);
[92b42442]347
[2f19103]348 assoc = udp_assoc_find_ref(repp);
[92b42442]349 if (assoc == NULL) {
[c3f7d37]350 log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
[92b42442]351 /* XXX Generate ICMP error. */
352 /* XXX Might propagate error directly by error return. */
[071a2c60]353 udp_msg_delete(msg);
[92b42442]354 return;
355 }
356
[fab2746]357 if (0) {
[2f19103]358 rc = udp_assoc_queue_msg(assoc, repp, msg);
[fab2746]359 if (rc != EOK) {
360 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
[ae7d03c]361 /* XXX Generate ICMP error? */
[fab2746]362 }
[92b42442]363 }
[fab2746]364
[c3f7d37]365 log_msg(LOG_DEFAULT, LVL_DEBUG, "call assoc->cb->recv_msg");
[2f19103]366 assoc->cb->recv_msg(assoc->cb_arg, repp, msg);
[99ea91b2]367 udp_assoc_delref(assoc);
[92b42442]368}
369
[141a20d]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
[b7fd2a0]383static errno_t udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
[92b42442]384 udp_msg_t *msg)
385{
386 udp_rcv_queue_entry_t *rqe;
387
[a1a101d]388 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
[2f19103]389 assoc, epp, msg);
[92b42442]390
391 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
392 if (rqe == NULL)
393 return ENOMEM;
394
395 link_initialize(&rqe->link);
[2f19103]396 rqe->epp = *epp;
[92b42442]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
[2f19103]408/** Find association structure for specified endpoint pair.
[92b42442]409 *
[2f19103]410 * An association is uniquely identified by an endpoint pair. Look up our
411 * association map and return association structure based on endpoint pair.
[92b42442]412 * The association reference count is bumped by one.
413 *
[2f19103]414 * @param epp Endpoint pair
[92b42442]415 * @return Association structure or NULL if not found.
416 */
[2f19103]417static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp)
[92b42442]418{
[b7fd2a0]419 errno_t rc;
[8d48c7e]420 void *arg;
421 udp_assoc_t *assoc;
[fab2746]422
[c3f7d37]423 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", epp);
[92b42442]424 fibril_mutex_lock(&assoc_list_lock);
[2f19103]425
[8d48c7e]426 rc = amap_find_match(amap, epp, &arg);
427 if (rc != EOK) {
[443a0bc]428 assert(rc == ENOENT);
[8d48c7e]429 fibril_mutex_unlock(&assoc_list_lock);
430 return NULL;
[92b42442]431 }
[2f19103]432
[8d48c7e]433 assoc = (udp_assoc_t *)arg;
434 udp_assoc_addref(assoc);
435
[92b42442]436 fibril_mutex_unlock(&assoc_list_lock);
[8d48c7e]437 return assoc;
[92b42442]438}
439
[ee603c4]440/**
441 * @}
442 */
Note: See TracBrowser for help on using the repository browser.