/
host.c
211 lines (158 loc) · 4.37 KB
/
host.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include "libknet-private.h"
int knet_host_get(knet_handle_t knet_h, uint16_t node_id, struct knet_host **host)
{
int ret;
if ((ret = pthread_rwlock_rdlock(&knet_h->list_rwlock)) != 0)
return ret;
*host = knet_h->host_index[node_id];
if (*host == NULL) {
pthread_rwlock_unlock(&knet_h->list_rwlock);
errno = ENOENT;
return ENOENT;
}
return 0;
}
int knet_host_acquire(knet_handle_t knet_h, struct knet_host **host)
{
int ret;
if ((ret = pthread_rwlock_rdlock(&knet_h->list_rwlock)) != 0)
return ret;
*host = knet_h->host_head;
return 0;
}
int knet_host_release(knet_handle_t knet_h, struct knet_host **host)
{
int ret;
*host = NULL;
if ((ret = pthread_rwlock_unlock(&knet_h->list_rwlock)) != 0)
return ret;
return 0;
}
int knet_host_foreach(knet_handle_t knet_h, knet_link_fn_t linkfun, struct knet_host_search *data)
{
int lockstatus;
struct knet_host *host;
lockstatus = pthread_rwlock_rdlock(&knet_h->list_rwlock);
if ((lockstatus != 0) && (lockstatus != EDEADLK))
return lockstatus;
for (host = knet_h->host_head; host != NULL; host = host->next) {
if ((linkfun(knet_h, host, data)) != KNET_HOST_FOREACH_NEXT)
break;
}
if (lockstatus == 0)
pthread_rwlock_unlock(&knet_h->list_rwlock);
return 0;
}
int knet_host_add(knet_handle_t knet_h, uint16_t node_id)
{
int link_idx, ret = 0; /* success */
struct knet_host *host;
if ((ret = pthread_rwlock_wrlock(&knet_h->list_rwlock)) != 0)
goto exit_clean;
if (knet_h->host_index[node_id] != NULL) {
errno = ret = EEXIST;
goto exit_unlock;
}
if ((host = malloc(sizeof(struct knet_host))) == NULL)
goto exit_unlock;
memset(host, 0, sizeof(struct knet_host));
host->node_id = node_id;
for (link_idx = 0; link_idx < KNET_MAX_LINK; link_idx++)
host->link[link_idx].link_id = link_idx;
/* adding new host to the index */
knet_h->host_index[node_id] = host;
if (!knet_h->host_head) {
knet_h->host_head = host;
knet_h->host_tail = host;
} else {
knet_h->host_tail->next = host;
knet_h->host_tail = host;
}
exit_unlock:
pthread_rwlock_unlock(&knet_h->list_rwlock);
exit_clean:
return ret;
}
int knet_host_remove(knet_handle_t knet_h, uint16_t node_id)
{
int ret = 0; /* success */
struct knet_host *host, *removed;
if ((ret = pthread_rwlock_wrlock(&knet_h->list_rwlock)) != 0)
goto exit_clean;
if (knet_h->host_index[node_id] == NULL) {
errno = ret = EINVAL;
goto exit_unlock;
}
removed = NULL;
/* removing host from list */
if (knet_h->host_head->node_id == node_id) {
removed = knet_h->host_head;
knet_h->host_head = removed->next;
} else {
for (host = knet_h->host_head; host->next != NULL; host = host->next) {
if (host->next->node_id == node_id) {
removed = host->next;
host->next = removed->next;
break;
}
}
}
if (removed != NULL) {
knet_h->host_index[node_id] = NULL;
free(removed);
}
exit_unlock:
pthread_rwlock_unlock(&knet_h->list_rwlock);
exit_clean:
return ret;
}
/* bcast = 0 -> unicast packet | 1 -> broadcast|mcast */
/* make this bcast/ucast aware */
int knet_should_deliver(struct knet_host *host, int bcast, seq_num_t seq_num)
{
size_t i, j; /* circular buffer indexes */
seq_num_t seq_dist;
char *dst_cbuf = NULL;
seq_num_t *dst_seq_num;
if (bcast) {
dst_cbuf = host->bcast_circular_buffer;
dst_seq_num = &host->bcast_seq_num_rx;
} else {
dst_cbuf = host->ucast_circular_buffer;
dst_seq_num = &host->ucast_seq_num_rx;
}
seq_dist = (seq_num < *dst_seq_num) ?
(SEQ_MAX - seq_num) + *dst_seq_num : *dst_seq_num - seq_num;
j = seq_num % KNET_CBUFFER_SIZE;
if (seq_dist < KNET_CBUFFER_SIZE) { /* seq num is in ring buffer */
return (dst_cbuf[j] == 0) ? 1 : 0;
} else if (seq_dist <= SEQ_MAX - KNET_CBUFFER_SIZE) {
memset(dst_cbuf, 0, KNET_CBUFFER_SIZE);
*dst_seq_num = seq_num;
}
/* cleaning up circular buffer */
i = (*dst_seq_num + 1) % KNET_CBUFFER_SIZE;
if (i > j) {
memset(dst_cbuf + i, 0, KNET_CBUFFER_SIZE - i);
memset(dst_cbuf, 0, j + 1);
} else {
memset(dst_cbuf + i, 0, j - i + 1);
}
*dst_seq_num = seq_num;
return 1;
}
void knet_has_been_delivered(struct knet_host *host, int bcast, seq_num_t seq_num)
{
if (bcast) {
host->bcast_circular_buffer[seq_num % KNET_CBUFFER_SIZE] = 1;
} else {
host->ucast_circular_buffer[seq_num % KNET_CBUFFER_SIZE] = 1;
}
return;
}