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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 648e2ac was 498ced1, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Unify reference counting and remove some unnecessary instances of <atomic.h>

  • 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 <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/** Create new association structure.
75 *
76 * @param epp Endpoint pair (will be copied)
77 * @param cb Callbacks
78 * @param cb_arg Callback argument
79 * @return New association or NULL
80 */
81udp_assoc_t *udp_assoc_new(inet_ep2_t *epp, udp_assoc_cb_t *cb, void *cb_arg)
82{
83 udp_assoc_t *assoc = NULL;
84
85 /* Allocate association structure */
86 assoc = calloc(1, sizeof(udp_assoc_t));
87 if (assoc == NULL)
88 goto error;
89
90 fibril_mutex_initialize(&assoc->lock);
91
92 /* One for the user */
93 refcount_init(&assoc->refcnt);
94
95 /* Initialize receive queue */
96 list_initialize(&assoc->rcv_queue);
97 fibril_condvar_initialize(&assoc->rcv_queue_cv);
98
99 if (epp != NULL)
100 assoc->ident = *epp;
101
102 assoc->cb = cb;
103 assoc->cb_arg = cb_arg;
104 return assoc;
105error:
106 return NULL;
107}
108
109/** Destroy association structure.
110 *
111 * Association structure should be destroyed when the folowing conditions
112 * are met:
113 * (1) user has deleted the association
114 * (2) nobody is holding references to the association
115 *
116 * This happens when @a assoc->refcnt is zero as we count (1)
117 * as an extra reference.
118 *
119 * @param assoc Association
120 */
121static void udp_assoc_free(udp_assoc_t *assoc)
122{
123 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_free(%p)", assoc->name, assoc);
124
125 while (!list_empty(&assoc->rcv_queue)) {
126 link_t *link = list_first(&assoc->rcv_queue);
127 udp_rcv_queue_entry_t *rqe = list_get_instance(link,
128 udp_rcv_queue_entry_t, link);
129 list_remove(link);
130
131 udp_msg_delete(rqe->msg);
132 free(rqe);
133 }
134
135 free(assoc);
136}
137
138/** Add reference to association.
139 *
140 * Increase association reference count by one.
141 *
142 * @param assoc Association
143 */
144void udp_assoc_addref(udp_assoc_t *assoc)
145{
146 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: upd_assoc_addref(%p)", assoc->name, assoc);
147 refcount_up(&assoc->refcnt);
148}
149
150/** Remove reference from association.
151 *
152 * Decrease association reference count by one.
153 *
154 * @param assoc Association
155 */
156void udp_assoc_delref(udp_assoc_t *assoc)
157{
158 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delref(%p)", assoc->name, assoc);
159
160 if (refcount_down(&assoc->refcnt))
161 udp_assoc_free(assoc);
162}
163
164/** Delete association.
165 *
166 * The caller promises not make no further references to @a assoc.
167 * UDP will free @a assoc eventually.
168 *
169 * @param assoc Association
170 */
171void udp_assoc_delete(udp_assoc_t *assoc)
172{
173 log_msg(LOG_DEFAULT, LVL_DEBUG, "%s: udp_assoc_delete(%p)", assoc->name, assoc);
174
175 assert(assoc->deleted == false);
176 udp_assoc_delref(assoc);
177 assoc->deleted = true;
178}
179
180/** Enlist association.
181 *
182 * Add association to the association map.
183 */
184errno_t udp_assoc_add(udp_assoc_t *assoc)
185{
186 inet_ep2_t aepp;
187 errno_t rc;
188
189 udp_assoc_addref(assoc);
190 fibril_mutex_lock(&assoc_list_lock);
191
192 rc = amap_insert(amap, &assoc->ident, assoc, af_allow_system, &aepp);
193 if (rc != EOK) {
194 udp_assoc_delref(assoc);
195 fibril_mutex_unlock(&assoc_list_lock);
196 return rc;
197 }
198
199 assoc->ident = aepp;
200 list_append(&assoc->link, &assoc_list);
201 fibril_mutex_unlock(&assoc_list_lock);
202
203 return EOK;
204}
205
206/** Delist association.
207 *
208 * Remove association from the association map.
209 */
210void udp_assoc_remove(udp_assoc_t *assoc)
211{
212 fibril_mutex_lock(&assoc_list_lock);
213 amap_remove(amap, &assoc->ident);
214 list_remove(&assoc->link);
215 fibril_mutex_unlock(&assoc_list_lock);
216 udp_assoc_delref(assoc);
217}
218
219/** Set IP link in association.
220 *
221 * @param assoc Association
222 * @param iplink IP link
223 */
224void udp_assoc_set_iplink(udp_assoc_t *assoc, service_id_t iplink)
225{
226 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_set_iplink(%p, %zu)",
227 assoc, iplink);
228 fibril_mutex_lock(&assoc->lock);
229 assoc->ident.local_link = iplink;
230 fibril_mutex_unlock(&assoc->lock);
231}
232
233/** Send message to association.
234 *
235 * @param assoc Association
236 * @param remote Remote endpoint or NULL not to override @a assoc
237 * @param msg Message
238 *
239 * @return EOK on success
240 * EINVAL if remote endpoint is not set
241 * ENOMEM if out of resources
242 * EIO if no route to destination exists
243 */
244errno_t udp_assoc_send(udp_assoc_t *assoc, inet_ep_t *remote, udp_msg_t *msg)
245{
246 udp_pdu_t *pdu;
247 inet_ep2_t epp;
248 errno_t rc;
249
250 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send(%p, %p, %p)",
251 assoc, remote, msg);
252
253 /* @a remote can be used to override the remote endpoint */
254 epp = assoc->ident;
255 if (remote != NULL)
256 epp.remote = *remote;
257
258 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check addr any");
259
260 if ((inet_addr_is_any(&epp.remote.addr)) ||
261 (epp.remote.port == inet_port_any))
262 return EINVAL;
263
264 /* This association has no local address set. Need to determine one. */
265 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check no local addr");
266 if (inet_addr_is_any(&epp.local.addr) && !assoc->nolocal) {
267 log_msg(LOG_DEFAULT, LVL_DEBUG, "Determine local address.");
268 rc = inet_get_srcaddr(&epp.remote.addr, 0, &epp.local.addr);
269 if (rc != EOK) {
270 log_msg(LOG_DEFAULT, LVL_DEBUG, "Cannot determine "
271 "local address.");
272 return EINVAL;
273 }
274 }
275
276 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - check version");
277
278 if (!inet_addr_is_any(&epp.local.addr) &&
279 epp.remote.addr.version != epp.local.addr.version)
280 return EINVAL;
281
282 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - encode pdu");
283
284 rc = udp_pdu_encode(&epp, msg, &pdu);
285 if (rc != EOK)
286 return ENOMEM;
287
288 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - transmit");
289
290 rc = udp_transmit_pdu(pdu);
291 udp_pdu_delete(pdu);
292
293 if (rc != EOK)
294 return EIO;
295
296 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_send - success");
297 return EOK;
298}
299
300/** Get a received message.
301 *
302 * Pull one message from the association's receive queue.
303 */
304errno_t udp_assoc_recv(udp_assoc_t *assoc, udp_msg_t **msg, inet_ep_t *remote)
305{
306 link_t *link;
307 udp_rcv_queue_entry_t *rqe;
308
309 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv()");
310
311 fibril_mutex_lock(&assoc->lock);
312 while (list_empty(&assoc->rcv_queue) && !assoc->reset) {
313 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - waiting");
314 fibril_condvar_wait(&assoc->rcv_queue_cv, &assoc->lock);
315 }
316
317 if (assoc->reset) {
318 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset");
319 fibril_mutex_unlock(&assoc->lock);
320 return ENXIO;
321 }
322
323 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - got a message");
324 link = list_first(&assoc->rcv_queue);
325 rqe = list_get_instance(link, udp_rcv_queue_entry_t, link);
326 list_remove(link);
327 fibril_mutex_unlock(&assoc->lock);
328
329 *msg = rqe->msg;
330 *remote = rqe->epp.remote;
331 free(rqe);
332
333 return EOK;
334}
335
336/** Message received.
337 *
338 * Find the association to which the message belongs and queue it.
339 */
340void udp_assoc_received(inet_ep2_t *repp, udp_msg_t *msg)
341{
342 udp_assoc_t *assoc;
343 errno_t rc;
344
345 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_received(%p, %p)", repp, msg);
346
347 assoc = udp_assoc_find_ref(repp);
348 if (assoc == NULL) {
349 log_msg(LOG_DEFAULT, LVL_DEBUG, "No association found. Message dropped.");
350 /* XXX Generate ICMP error. */
351 /* XXX Might propagate error directly by error return. */
352 udp_msg_delete(msg);
353 return;
354 }
355
356 if (0) {
357 rc = udp_assoc_queue_msg(assoc, repp, msg);
358 if (rc != EOK) {
359 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped.");
360 /* XXX Generate ICMP error? */
361 }
362 }
363
364 log_msg(LOG_DEFAULT, LVL_DEBUG, "call assoc->cb->recv_msg");
365 assoc->cb->recv_msg(assoc->cb_arg, repp, msg);
366 udp_assoc_delref(assoc);
367}
368
369/** Reset association.
370 *
371 * This causes any pendingreceive operations to return immediately with
372 * UDP_ERESET.
373 */
374void udp_assoc_reset(udp_assoc_t *assoc)
375{
376 fibril_mutex_lock(&assoc->lock);
377 assoc->reset = true;
378 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
379 fibril_mutex_unlock(&assoc->lock);
380}
381
382static errno_t udp_assoc_queue_msg(udp_assoc_t *assoc, inet_ep2_t *epp,
383 udp_msg_t *msg)
384{
385 udp_rcv_queue_entry_t *rqe;
386
387 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_queue_msg(%p, %p, %p)",
388 assoc, epp, msg);
389
390 rqe = calloc(1, sizeof(udp_rcv_queue_entry_t));
391 if (rqe == NULL)
392 return ENOMEM;
393
394 link_initialize(&rqe->link);
395 rqe->epp = *epp;
396 rqe->msg = msg;
397
398 fibril_mutex_lock(&assoc->lock);
399 list_append(&rqe->link, &assoc->rcv_queue);
400 fibril_mutex_unlock(&assoc->lock);
401
402 fibril_condvar_broadcast(&assoc->rcv_queue_cv);
403
404 return EOK;
405}
406
407/** Find association structure for specified endpoint pair.
408 *
409 * An association is uniquely identified by an endpoint pair. Look up our
410 * association map and return association structure based on endpoint pair.
411 * The association reference count is bumped by one.
412 *
413 * @param epp Endpoint pair
414 * @return Association structure or NULL if not found.
415 */
416static udp_assoc_t *udp_assoc_find_ref(inet_ep2_t *epp)
417{
418 errno_t rc;
419 void *arg;
420 udp_assoc_t *assoc;
421
422 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", epp);
423 fibril_mutex_lock(&assoc_list_lock);
424
425 rc = amap_find_match(amap, epp, &arg);
426 if (rc != EOK) {
427 assert(rc == ENOENT);
428 fibril_mutex_unlock(&assoc_list_lock);
429 return NULL;
430 }
431
432 assoc = (udp_assoc_t *)arg;
433 udp_assoc_addref(assoc);
434
435 fibril_mutex_unlock(&assoc_list_lock);
436 return assoc;
437}
438
439/**
440 * @}
441 */
Note: See TracBrowser for help on using the repository browser.