Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*	$NetBSD: isns_task.c,v 1.1.1.1 2011/01/16 01:22:50 agc Exp $	*/

/*-
 * Copyright (c) 2004,2009 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Wasabi Systems, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: isns_task.c,v 1.1.1.1 2011/01/16 01:22:50 agc Exp $");

/*
 * isns_task.c
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "isns.h"
#include "isns_config.h"

static struct iovec write_buf[2 + (ISNS_MAX_PDU_PAYLOAD / ISNS_BUF_SIZE) +
    ((ISNS_MAX_PDU_PAYLOAD % ISNS_BUF_SIZE) != 0)];

static isns_task_handler isns_task_discover_server;
static isns_task_handler isns_task_reconnect_server;
static isns_task_handler isns_task_send_pdu;
static isns_task_handler isns_task_init_socket_io;
static isns_task_handler isns_task_init_refresh;


void
isns_run_task(struct isns_task_s *task_p)
{
	static isns_task_handler *task_dispatch_table[ISNS_NUM_TASKS] = {
		isns_task_discover_server,
		isns_task_reconnect_server,
		isns_task_send_pdu,
		isns_task_init_socket_io,
		isns_task_init_refresh
	};

	DBG("isns_run_task: task_type=%d\n", task_p->task_type);

	if (task_p->task_type < ARRAY_ELEMS(task_dispatch_table))
		task_dispatch_table[task_p->task_type](task_p);
	else
		DBG("isns_run_task: unknown task type=%d\n", task_p->task_type);
}


int
isns_wait_task(struct isns_task_s *task_p, const struct timespec *timeout_p)
{
	struct timeval tv_now;
	struct timespec ts_abstime;
	int rval;

	DBG("isns_wait_task: waitable=%d\n", task_p->waitable);

	if (!task_p->waitable)
		return EPERM;

	pthread_mutex_lock(&task_p->wait_mutex);

	if (timeout_p == NULL) {
		rval = pthread_cond_wait(&task_p->wait_condvar,
		    &task_p->wait_mutex);
	} else {
		gettimeofday(&tv_now, NULL);
		TIMEVAL_TO_TIMESPEC(&tv_now, &ts_abstime);
		timespecadd(&ts_abstime, timeout_p, &ts_abstime);

		rval = pthread_cond_timedwait(&task_p->wait_condvar,
		    &task_p->wait_mutex, &ts_abstime);
	}

	pthread_mutex_unlock(&task_p->wait_mutex);

	isns_free_task(task_p);

	DBG("isns_wait_task: wait done (rval=%d)\n", rval);

	return rval;
}


void
isns_end_task(struct isns_task_s *task_p)
{
	DBG("isns_end_task: %p\n", task_p);
	if (task_p == task_p->cfg_p->curtask_p)
		task_p->cfg_p->curtask_p = NULL;

	if (task_p->waitable)
		pthread_cond_signal(&task_p->wait_condvar);

	isns_free_task(task_p);
}


static void
isns_task_discover_server(struct isns_task_s *task_p)
{
	/* discover server here */
	DBG("isns_task_discover_server: entered\n");

	isns_end_task(task_p);
}


/*
 * isns_task_reconnect_server()
 */
static void
isns_task_reconnect_server(struct isns_task_s *task_p)
{
	struct addrinfo *ai_p;
	int rv;


	DBG("isns_task_reconnect_server: entered\n");
	
	ai_p = task_p->var.reconnect_server.ai_p;

	rv = isns_socket_create(&(task_p->cfg_p->sd), ai_p->ai_family,
	    ai_p->ai_socktype);
	if (rv != 0)
		return;

	rv = isns_socket_connect(task_p->cfg_p->sd, ai_p->ai_addr,
	    ai_p->ai_addrlen);
	if (rv != 0) {
		/* Add ISNS_EVT_TIMER_RECON to kqueue */
		rv = isns_change_kevent_list(task_p->cfg_p,
		    (uintptr_t)ISNS_EVT_TIMER_RECON, EVFILT_TIMER, EV_ADD,
		    (int64_t)ISNS_EVT_TIMER_RECON_PERIOD_MS,
		    (intptr_t)isns_kevent_timer_recon);
		if (rv == -1)
			DBG("isns_task_reconnect_server: error on "
			    "isns_change_kevent_list(1)\n");
	} else {
		task_p->cfg_p->sd_connected = 1;

		/* Add cfg_p->sd to kqueue */
		rv = isns_change_kevent_list(task_p->cfg_p,
		    (uintptr_t)(task_p->cfg_p->sd), EVFILT_READ,
		    EV_ADD | EV_CLEAR, (int64_t)0,
		    (intptr_t)isns_kevent_socket);
		if (rv == -1)
			DBG("isns_task_reconnect_server: error on "
			    "isns_change_kevent_lists(2)\n");

		isns_end_task(task_p);
	}
}

/*
 * isns_task_send_pdu()
 *
 * We send all of the pdu's associated with transaction task_p->trans_p here.
 *
 * Assumptions:
 *	(1) task_p->trans_p->pdu_req_list is an ordered (seq_id) list of
 *	    related (trans_id), appropriately sized pdus to be sent. The first
 *	    pdu has flag ISNS_FLAG_FIRST_PDU set and the last pdu has flag
 *	    ISNS_FLAG_LAST_PDU set.
 */
static void
isns_task_send_pdu(struct isns_task_s *task_p)
{
	struct iovec *iovp;
	struct isns_config_s *cfg_p;
	struct isns_pdu_s *pdu_p; /* points to first pdu in pdu_req_list */
	struct isns_buffer_s *buf_p;
	ssize_t bytes_written;
	ssize_t count;
	size_t bytes_to_write;
	int iovcnt, cur_iovec;
	char *ptr;


	DBG("isns_task_send_pdu: entered\n");

	cfg_p = task_p->cfg_p;
	pdu_p = task_p->var.send_pdu.pdu_p;

	while (pdu_p != NULL) {
		/* adjust byte order if necessary */
		if (pdu_p->byteorder_host) {
			pdu_p->hdr.isnsp_version = isns_htons(pdu_p->hdr.
			    isnsp_version);
			pdu_p->hdr.func_id = isns_htons(pdu_p->hdr.func_id);
			pdu_p->hdr.payload_len = isns_htons(pdu_p->hdr.
			    payload_len);
			pdu_p->hdr.flags = isns_htons(pdu_p->hdr.flags);
			pdu_p->hdr.trans_id = isns_htons(pdu_p->hdr.trans_id);
			pdu_p->hdr.seq_id = isns_htons(pdu_p->hdr.seq_id);

			pdu_p->byteorder_host = 0;
		}
		DUMP_PDU(pdu_p);

		/* send PDU via socket here */
		write_buf[0].iov_base = &(pdu_p->hdr);
		write_buf[0].iov_len = sizeof(pdu_p->hdr);
		bytes_to_write = write_buf[0].iov_len;
		iovcnt = 1;

		buf_p = pdu_p->payload_p;
		while (buf_p != NULL) {
			write_buf[iovcnt].iov_base = isns_buffer_data(buf_p,0);
			write_buf[iovcnt].iov_len = buf_p->cur_len;
			bytes_to_write += write_buf[iovcnt].iov_len;
			iovcnt++;
			buf_p = buf_p->next;
		}

		/* iovcnt and bytes_to_write are initialized */ 
		cur_iovec = 0;
		buf_p = ((struct isns_buffer_s *)(void *)pdu_p) - 1;
		do {
			iovp = &(write_buf[cur_iovec]);
			bytes_written = isns_socket_writev(cfg_p->sd, iovp,
			    iovcnt);
			if (bytes_written == -1) {
				DBG("isns_task_send_pdu: error on "
			    	"isns_socket_writev\n");
				isns_socket_close(cfg_p->sd);
				cfg_p->sd_connected = 0;

				isns_process_connection_loss(cfg_p);

				if (cfg_p->pdu_in_p != NULL) {
					isns_free_pdu(cfg_p->pdu_in_p);
					cfg_p->pdu_in_p = NULL;
				}
			
				break;
			}

			if (bytes_written < (ssize_t)bytes_to_write) {
				count = bytes_written;
				while (buf_p != NULL) { /* -OR- while (1) */
					if ((unsigned)count >= write_buf[
					    cur_iovec].iov_len) {
						count -= write_buf[cur_iovec].
						    iov_len;
						if (cur_iovec == 0)
							buf_p = pdu_p->
							    payload_p; 
						else
							buf_p = buf_p->next;
						cur_iovec++;
						iovcnt--;

						if (count == 0) {
							/* Do another write */
							break;
						} else {
							/* Look at new iovec */ 
							continue;
						}
					} else {
						write_buf[cur_iovec].iov_len -=
						    count;

						ptr = (char *) write_buf[cur_iovec].iov_base;
						ptr += count;
						write_buf[cur_iovec].iov_base = ptr;

						/* Do another write */
						break;
					}
				}
			}

			bytes_to_write -= bytes_written;
		} while (bytes_to_write);

		pdu_p = pdu_p->next;
	}

	if (!task_p->waitable) {
		isns_complete_trans(task_p->var.send_pdu.trans_p);
		isns_end_task(task_p);
	}
}

/*
 * isns_task_init_socket_io()
 */
static void
isns_task_init_socket_io(struct isns_task_s *task_p)
{
	struct isns_config_s *cfg_p;
	int rv;


	DBG("isns_task_init_socket_io: entered\n");

	cfg_p = task_p->cfg_p;

	if (cfg_p->sd_connected) {
		isns_socket_close(cfg_p->sd);
		cfg_p->sd_connected = 0;

		/* We may have received part of an unsolicited/duplicate pdu */
		if (cfg_p->pdu_in_p != NULL) {
			isns_free_pdu(cfg_p->pdu_in_p);
			cfg_p->pdu_in_p = NULL;
		}
	}

	/* May have an allocated 'struct addrinfo', whether connected or not */
	if (cfg_p->ai_p != NULL) {
		isns_free(cfg_p->ai_p);
		cfg_p->ai_p = NULL;
	}

	cfg_p->sd = task_p->var.init_socket_io.sd;
	cfg_p->ai_p = task_p->var.init_socket_io.ai_p;

	cfg_p->sd_connected = 1;

	/* Add cfg_p->sd to kqueue */
	rv = isns_change_kevent_list(cfg_p, (uintptr_t)cfg_p->sd,
	    EVFILT_READ, EV_ADD | EV_CLEAR, (int64_t)0,
	    (intptr_t)isns_kevent_socket);
	if (rv == -1)
		DBG("isns_task_init_socket_io: error on "
		    "isns_change_kevent_list\n");

	isns_end_task(task_p);
}


/*
 * isns_task_init_refresh(struct isns_task_s *task_p)
 */
static void
isns_task_init_refresh(struct isns_task_s *task_p)
{
	struct isns_config_s *cfg_p;
	int rval;

	DBG("isns_task_init_refresh: entered\n");

	/* Free any previous refresh info. */
	cfg_p = task_p->cfg_p;
	if (cfg_p->refresh_p != NULL) {
		if (cfg_p->refresh_p->trans_p != NULL)
			isns_free_trans(cfg_p->refresh_p->trans_p);
		isns_free(cfg_p->refresh_p);
	}

	/* Assign new refresh info into config struct. */
	cfg_p->refresh_p = task_p->var.init_refresh.ref_p;
	cfg_p->refresh_p->trans_p = NULL;

	/* Setup (or change) kevent timer for reg refresh. */
	rval = isns_change_kevent_list(cfg_p,
	    (uintptr_t)ISNS_EVT_TIMER_REFRESH, EVFILT_TIMER,
	    EV_ADD | EV_ENABLE, (int64_t)cfg_p->refresh_p->interval * 1000,
	    (intptr_t)isns_kevent_timer_refresh);
	if (rval == -1) {
		DBG("isns_task_init_refresh: "
		    "error on isns_change_kevent_list()\n");
	}

	isns_end_task(task_p);
}


struct isns_task_s *
isns_new_task(struct isns_config_s *cfg_p, uint8_t task_type, int waitable)
{
	struct isns_buffer_s *buf_p;
	struct isns_task_s *task_p;
	pthread_mutexattr_t mutexattr;
	pthread_condattr_t condattr;

	task_p = NULL;
	buf_p = isns_new_buffer((int)sizeof(struct isns_task_s));
	if (buf_p) {
		task_p = (struct isns_task_s *)isns_buffer_data(buf_p, 0);
		task_p->cfg_p = cfg_p;
		task_p->task_type = task_type;
		task_p->waitable = waitable;

		if (waitable) {
			pthread_mutexattr_init(&mutexattr);
			pthread_mutexattr_settype(&mutexattr,
			    ISNS_MUTEX_TYPE_NORMAL);
			pthread_mutex_init(&task_p->wait_mutex, &mutexattr);

			pthread_condattr_init(&condattr);
			pthread_cond_init(&task_p->wait_condvar, &condattr);
			task_p->wait_ref_count = 2;
		}
	}

	DBG("isns_new_task: %p, waitable=%d\n", task_p, waitable);

	return task_p;
}


void
isns_free_task(struct isns_task_s *task_p)
{
	struct isns_buffer_s *buf_p;
	int ref_count;

	DBG("isns_free_task: %p\n", task_p);
	if (task_p->waitable) {
		pthread_mutex_lock(&task_p->wait_mutex);
		ref_count = --task_p->wait_ref_count;
		pthread_mutex_unlock(&task_p->wait_mutex);

		if (ref_count > 0) {
			DBG("isns_free_task: ref_count > 0, no free done\n");
			return;
		}

		pthread_mutex_destroy(&task_p->wait_mutex);
		pthread_cond_destroy(&task_p->wait_condvar);
	}
	buf_p = ((struct isns_buffer_s *)(void *)(task_p))-1;
	isns_free_buffer(buf_p);
}


void
isns_taskq_insert_head(struct isns_config_s *cfg_p,
    struct isns_task_s *task_p)
{
	pthread_mutex_lock(&cfg_p->taskq_mutex);
	SIMPLEQ_INSERT_HEAD(&cfg_p->taskq_head, task_p, taskq_entry);
	pthread_mutex_unlock(&cfg_p->taskq_mutex);

	DBG("isns_taskq_insert_head: %p\n", task_p);
}


void
isns_taskq_insert_tail(struct isns_config_s *cfg_p,
    struct isns_task_s *task_p)
{
	pthread_mutex_lock(&cfg_p->taskq_mutex);
	SIMPLEQ_INSERT_TAIL(&cfg_p->taskq_head, task_p, taskq_entry);
	pthread_mutex_unlock(&cfg_p->taskq_mutex);

	DBG("isns_taskq_insert_tail: %p\n", task_p);
}


struct isns_task_s *
isns_taskq_remove(struct isns_config_s *cfg_p)
{
	struct isns_task_s *task_p = NULL;

	pthread_mutex_lock(&cfg_p->taskq_mutex);
	if ((task_p = SIMPLEQ_FIRST(&cfg_p->taskq_head)) != NULL)
		SIMPLEQ_REMOVE_HEAD(&cfg_p->taskq_head, taskq_entry);
	pthread_mutex_unlock(&cfg_p->taskq_mutex);

	DBG("isns_taskq_remove: %p\n", task_p);

	return task_p;
}


struct isns_task_s *
isns_taskq_remove_trans(struct isns_config_s *cfg_p, uint16_t trans_id)
{
	struct isns_task_s *task_p;
	int trans_found;

	trans_found = 0;
	pthread_mutex_lock(&cfg_p->taskq_mutex);
	SIMPLEQ_FOREACH(task_p, &cfg_p->taskq_head, taskq_entry) {
		if ((task_p->task_type == ISNS_TASK_SEND_PDU)
		    && (task_p->var.send_pdu.trans_p->id == trans_id)) {
			trans_found = 1;
			break;
		}
	}
	if (trans_found) {
		SIMPLEQ_REMOVE(&cfg_p->taskq_head, task_p, isns_task_s,
		    taskq_entry);
	}
	pthread_mutex_unlock(&cfg_p->taskq_mutex);

	return (trans_found ? task_p : NULL);
}