[c5808b41] | 1 | /*
|
---|
[2f19103] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[c5808b41] | 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 | /**
|
---|
[032bbe7] | 34 | * @file Global segment receive queue
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <adt/prodcons.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <io/log.h>
|
---|
[d14840d] | 40 | #include <stdbool.h>
|
---|
[c5808b41] | 41 | #include <stdlib.h>
|
---|
[88a6819] | 42 | #include <fibril.h>
|
---|
[d14840d] | 43 | #include <fibril_synch.h>
|
---|
[032bbe7] | 44 | #include "conn.h"
|
---|
[c5808b41] | 45 | #include "rqueue.h"
|
---|
[eea65f4] | 46 | #include "segment.h"
|
---|
[c5808b41] | 47 | #include "tcp_type.h"
|
---|
[762b48a] | 48 | #include "ucall.h"
|
---|
[c5808b41] | 49 |
|
---|
| 50 | static prodcons_t rqueue;
|
---|
[d14840d] | 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;
|
---|
[c5808b41] | 55 |
|
---|
[032bbe7] | 56 | /** Initialize segment receive queue. */
|
---|
[d14840d] | 57 | void tcp_rqueue_init(tcp_rqueue_cb_t *rcb)
|
---|
[c5808b41] | 58 | {
|
---|
| 59 | prodcons_initialize(&rqueue);
|
---|
[d14840d] | 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);
|
---|
[c5808b41] | 78 | }
|
---|
| 79 |
|
---|
[032bbe7] | 80 | /** Insert segment into receive queue.
|
---|
| 81 | *
|
---|
[2f19103] | 82 | * @param epp Endpoint pair, oriented for reception
|
---|
[9520af7] | 83 | * @param seg Segment (ownership transferred to rqueue)
|
---|
[032bbe7] | 84 | */
|
---|
[2f19103] | 85 | void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
[c5808b41] | 86 | {
|
---|
| 87 | tcp_rqueue_entry_t *rqe;
|
---|
| 88 |
|
---|
[d14840d] | 89 | log_msg(LOG_DEFAULT, LVL_DEBUG2, "tcp_rqueue_insert_seg()");
|
---|
| 90 |
|
---|
| 91 | if (seg != NULL)
|
---|
| 92 | tcp_segment_dump(seg);
|
---|
[6896409c] | 93 |
|
---|
[c5808b41] | 94 | rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
|
---|
| 95 | if (rqe == NULL) {
|
---|
[a1a101d] | 96 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating RQE.");
|
---|
[c5808b41] | 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[2f19103] | 100 | rqe->epp = *epp;
|
---|
[c5808b41] | 101 | rqe->seg = seg;
|
---|
| 102 |
|
---|
| 103 | prodcons_produce(&rqueue, &rqe->link);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[88a6819] | 106 | /** Receive queue handler fibril. */
|
---|
[b7fd2a0] | 107 | static errno_t tcp_rqueue_fibril(void *arg)
|
---|
[c5808b41] | 108 | {
|
---|
| 109 | link_t *link;
|
---|
| 110 | tcp_rqueue_entry_t *rqe;
|
---|
| 111 |
|
---|
[a1a101d] | 112 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril()");
|
---|
[c5808b41] | 113 |
|
---|
| 114 | while (true) {
|
---|
| 115 | link = prodcons_consume(&rqueue);
|
---|
| 116 | rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
|
---|
| 117 |
|
---|
[d14840d] | 118 | if (rqe->seg == NULL) {
|
---|
| 119 | free(rqe);
|
---|
| 120 | break;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | rqueue_cb->seg_received(&rqe->epp, rqe->seg);
|
---|
[2cb32f9] | 124 | free(rqe);
|
---|
[c5808b41] | 125 | }
|
---|
[88a6819] | 126 |
|
---|
[d14840d] | 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 |
|
---|
[88a6819] | 135 | return 0;
|
---|
[c5808b41] | 136 | }
|
---|
| 137 |
|
---|
[88a6819] | 138 | /** Start receive queue handler fibril. */
|
---|
| 139 | void tcp_rqueue_fibril_start(void)
|
---|
[c5808b41] | 140 | {
|
---|
[88a6819] | 141 | fid_t fid;
|
---|
[c5808b41] | 142 |
|
---|
[a1a101d] | 143 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril_start()");
|
---|
[c5808b41] | 144 |
|
---|
[88a6819] | 145 | fid = fibril_create(tcp_rqueue_fibril, NULL);
|
---|
| 146 | if (fid == 0) {
|
---|
[a1a101d] | 147 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating rqueue fibril.");
|
---|
[c5808b41] | 148 | return;
|
---|
| 149 | }
|
---|
[88a6819] | 150 |
|
---|
| 151 | fibril_add_ready(fid);
|
---|
[d14840d] | 152 | fibril_active = true;
|
---|
[c5808b41] | 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * @}
|
---|
| 157 | */
|
---|