| 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 tcp
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file Global segment receive queue
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <adt/prodcons.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <io/log.h>
|
|---|
| 40 | #include <stdbool.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| 42 | #include <fibril.h>
|
|---|
| 43 | #include <fibril_synch.h>
|
|---|
| 44 | #include "conn.h"
|
|---|
| 45 | #include "rqueue.h"
|
|---|
| 46 | #include "segment.h"
|
|---|
| 47 | #include "tcp_type.h"
|
|---|
| 48 | #include "ucall.h"
|
|---|
| 49 |
|
|---|
| 50 | static prodcons_t rqueue;
|
|---|
| 51 | static bool fibril_active;
|
|---|
| 52 | static fibril_mutex_t lock;
|
|---|
| 53 | static fibril_condvar_t cv;
|
|---|
| 54 | static tcp_rqueue_cb_t *rqueue_cb;
|
|---|
| 55 |
|
|---|
| 56 | /** Initialize segment receive queue. */
|
|---|
| 57 | void tcp_rqueue_init(tcp_rqueue_cb_t *rcb)
|
|---|
| 58 | {
|
|---|
| 59 | prodcons_initialize(&rqueue);
|
|---|
| 60 | fibril_mutex_initialize(&lock);
|
|---|
| 61 | fibril_condvar_initialize(&cv);
|
|---|
| 62 | fibril_active = false;
|
|---|
| 63 | rqueue_cb = rcb;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /** Finalize segment receive queue. */
|
|---|
| 67 | void tcp_rqueue_fini(void)
|
|---|
| 68 | {
|
|---|
| 69 | inet_ep2_t epp;
|
|---|
| 70 |
|
|---|
| 71 | inet_ep2_init(&epp);
|
|---|
| 72 | tcp_rqueue_insert_seg(&epp, NULL);
|
|---|
| 73 |
|
|---|
| 74 | fibril_mutex_lock(&lock);
|
|---|
| 75 | while (fibril_active)
|
|---|
| 76 | fibril_condvar_wait(&cv, &lock);
|
|---|
| 77 | fibril_mutex_unlock(&lock);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /** Insert segment into receive queue.
|
|---|
| 81 | *
|
|---|
| 82 | * @param epp Endpoint pair, oriented for reception
|
|---|
| 83 | * @param seg Segment (ownership transferred to rqueue)
|
|---|
| 84 | */
|
|---|
| 85 | void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
|---|
| 86 | {
|
|---|
| 87 | tcp_rqueue_entry_t *rqe;
|
|---|
| 88 |
|
|---|
| 89 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_insert_seg()");
|
|---|
| 90 |
|
|---|
| 91 | if (seg != NULL)
|
|---|
| 92 | tcp_segment_dump(seg);
|
|---|
| 93 |
|
|---|
| 94 | rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
|
|---|
| 95 | if (rqe == NULL) {
|
|---|
| 96 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating RQE.");
|
|---|
| 97 | return;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | rqe->epp = *epp;
|
|---|
| 101 | rqe->seg = seg;
|
|---|
| 102 |
|
|---|
| 103 | prodcons_produce(&rqueue, &rqe->link);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Receive queue handler fibril. */
|
|---|
| 107 | static errno_t tcp_rqueue_fibril(void *arg)
|
|---|
| 108 | {
|
|---|
| 109 | link_t *link;
|
|---|
| 110 | tcp_rqueue_entry_t *rqe;
|
|---|
| 111 |
|
|---|
| 112 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril()");
|
|---|
| 113 |
|
|---|
| 114 | while (true) {
|
|---|
| 115 | link = prodcons_consume(&rqueue);
|
|---|
| 116 | rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
|
|---|
| 117 |
|
|---|
| 118 | if (rqe->seg == NULL) {
|
|---|
| 119 | free(rqe);
|
|---|
| 120 | break;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | rqueue_cb->seg_received(&rqe->epp, rqe->seg);
|
|---|
| 124 | free(rqe);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_fibril() exiting");
|
|---|
| 128 |
|
|---|
| 129 | /* Finished */
|
|---|
| 130 | fibril_mutex_lock(&lock);
|
|---|
| 131 | fibril_active = false;
|
|---|
| 132 | fibril_mutex_unlock(&lock);
|
|---|
| 133 | fibril_condvar_broadcast(&cv);
|
|---|
| 134 |
|
|---|
| 135 | return 0;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** Start receive queue handler fibril. */
|
|---|
| 139 | void tcp_rqueue_fibril_start(void)
|
|---|
| 140 | {
|
|---|
| 141 | fid_t fid;
|
|---|
| 142 |
|
|---|
| 143 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril_start()");
|
|---|
| 144 |
|
|---|
| 145 | fid = fibril_create(tcp_rqueue_fibril, NULL);
|
|---|
| 146 | if (fid == 0) {
|
|---|
| 147 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating rqueue fibril.");
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | fibril_add_ready(fid);
|
|---|
| 152 | fibril_active = true;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * @}
|
|---|
| 157 | */
|
|---|