|
root / base / usr / src / cmd / isns / isnsd / esi.c
esi.c C 1476 lines 29.9 KB
   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
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <signal.h>
#include <poll.h>

#include "isns_server.h"
#include "isns_cache.h"
#include "isns_obj.h"
#include "isns_pdu.h"
#include "isns_func.h"
#include "isns_qry.h"
#include "isns_msgq.h"
#include "isns_log.h"
#include "isns_sched.h"
#include "isns_scn.h"
#include "isns_esi.h"

/*
 * global variables.
 */

/*
 * local variables.
 */
static ev_t *ev_list = NULL;

static uint32_t stopwatch = 0;
static pthread_mutex_t stw_mtx = PTHREAD_MUTEX_INITIALIZER;

static int wakeup = 0;
static pthread_mutex_t idl_mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t idl_cond = PTHREAD_COND_INITIALIZER;

/*
 * external variables.
 */
extern const int UID_ATTR_INDEX[MAX_OBJ_TYPE_FOR_SIZE];

extern boolean_t time_to_exit;

extern msg_queue_t *sys_q;

extern uint64_t esi_threshold;

#ifdef DEBUG
extern void dump_pdu1(isns_pdu_t *);
#endif

/*
 * local functions.
 */
static void *esi_monitor(void *);

/*
 * ****************************************************************************
 *
 * new_esi_portal:
 *	Make a new portal for ESI event.
 *
 * uid	- the portal object UID.
 * ip6	- the portal IPv6 format IP address.
 * port	- the portal port.
 * esip	- the ESI port.
 * return - the new ESI portal.
 *
 * ****************************************************************************
 */
static esi_portal_t *
new_esi_portal(
	uint32_t uid,
	in6_addr_t *ip6,
	uint32_t port,
	uint32_t esip
)
{
	esi_portal_t *p;

	p = (esi_portal_t *)malloc(sizeof (esi_portal_t));
	if (p != NULL) {
		if (((int *)ip6)[0] == 0x00 &&
		    ((int *)ip6)[1] == 0x00 &&
		    ((uchar_t *)ip6)[8] == 0x00 &&
		    ((uchar_t *)ip6)[9] == 0x00 &&
		    ((uchar_t *)ip6)[10] == 0xFF &&
		    ((uchar_t *)ip6)[11] == 0xFF) {
			p->sz = sizeof (in_addr_t);
			p->ip4 = ((uint32_t *)ip6)[3];
		} else {
			p->sz = sizeof (in6_addr_t);
		}
		p->ip6 = ip6;
		p->port = port;
		p->esip = esip;
		p->ref = uid;
		p->so = 0;
		p->next = NULL;
	}

	return (p);
}

/*
 * ****************************************************************************
 *
 * free_esi_portal:
 *	Free a list of portal of one ESI event.
 *
 * p	- the ESI portal.
 *
 * ****************************************************************************
 */
static void
free_esi_portal(
	esi_portal_t *p
)
{
	esi_portal_t *n;

	while (p != NULL) {
		n = p->next;
		free(p->ip6);
		free(p);
		p = n;
	}
}

/*
 * ****************************************************************************
 *
 * ev_new:
 *	Make a new ESI event.
 *
 * uid	- the Entity object UID.
 * eid	- the Entity object name.
 * len	- the length of the name.
 * return - the ESI event.
 *
 * ****************************************************************************
 */
static ev_t *
ev_new(
	uint32_t uid,
	uchar_t *eid,
	uint32_t len
)
{
	ev_t *ev;

	ev = (ev_t *)malloc(sizeof (ev_t));
	if (ev != NULL) {
		if (pthread_mutex_init(&ev->mtx, NULL) != 0 ||
		    (ev->eid = (uchar_t *)malloc(len)) == NULL) {
			free(ev);
			return (NULL);
		}
		ev->uid = uid;
		(void) strcpy((char *)ev->eid, (char *)eid);
		ev->eid_len = len;
		/* initialization time */
		ev->flags = EV_FLAG_INIT;
	}

	return (ev);
}

/*
 * ****************************************************************************
 *
 * cb_portal_uids:
 *	Callback function which makes a copy of the portal child object
 *	UIDs from a Network Entity object.
 *
 * p1	- the Network Entity object.
 * p2	- the lookup control data.
 * return - the number of portal object UIDs.
 *
 * ****************************************************************************
 */
static int
cb_portal_uids(
	void *p1,
	void *p2
)
{
	isns_obj_t *obj = (isns_obj_t *)p1;
	lookup_ctrl_t *lcp = (lookup_ctrl_t *)p2;

	isns_attr_t *attr;

	uint32_t *cuidp;

	uint32_t num = 0;
	uint32_t *p = NULL;

	cuidp = get_child_t(obj, OBJ_PORTAL);
	if (cuidp != NULL) {
		p = (uint32_t *)malloc(*cuidp * sizeof (*p));
		if (p != NULL) {
			num = *cuidp ++;
			(void) memcpy(p, cuidp, num * sizeof (*p));
			lcp->data[1].ptr = (uchar_t *)p;
		}
	}

	attr = &obj->attrs[ATTR_INDEX_ENTITY(ISNS_ENTITY_REG_PERIOD_ATTR_ID)];
	if (attr->tag != 0 && attr->value.ui != 0) {
		lcp->data[2].ui = attr->value.ui;
	} else {
		/* just one second before the end of the world */
		lcp->data[2].ui = INFINITY - 1;
	}

	return (num);
}

/*
 * ****************************************************************************
 *
 * cb_esi_portal:
 *	Callback function which gets ESI port number and ESI interval
 *	from a portal object.
 *
 * p1	- the Portal object.
 * p2	- the lookup control data.
 * return - the ESI interval.
 *
 * ****************************************************************************
 */
static int
cb_esi_portal(
	void *p1,
	void *p2
)
{
	uint32_t intval = 0;

	isns_obj_t *obj;
	lookup_ctrl_t *lcp;

	in6_addr_t *ip;
	uint32_t esip;

	isns_attr_t *attr;

	if (cb_clone_attrs(p1, p2) == 0) {
		obj = (isns_obj_t *)p1;
		lcp = (lookup_ctrl_t *)p2;
		ip = lcp->data[1].ip;
		esip = lcp->data[2].ui;
		if (esip != 0) {
			attr = &obj->attrs[ATTR_INDEX_PORTAL(
			    ISNS_PORTAL_PORT_ATTR_ID)];
			lcp->data[0].ui = attr->value.ui;
			attr = &obj->attrs[ATTR_INDEX_PORTAL(
			    ISNS_ESI_INTERVAL_ATTR_ID)];
			if (attr->tag != 0 && attr->value.ui != 0) {
				intval = attr->value.ui;
			} else {
				intval = DEFAULT_ESI_INTVAL;
			}
		} else {
			free(ip);
		}
	}

	return ((int)intval);
}

/*
 * ****************************************************************************
 *
 * extract_esi_portal:
 *	Extract a list of portal which have an ESI port for an Entity.
 *
 * uid	- the Entity object UID.
 * intval - the ESI interval for returnning.
 * return - the list of portals.
 *
 * ****************************************************************************
 */
static esi_portal_t *
extract_esi_portal(
	uint32_t uid,
	uint32_t *intval
)
{
	esi_portal_t *list = NULL;
	esi_portal_t *p;

	lookup_ctrl_t lc;

	uint32_t num_of_portal;
	uint32_t *portal_uids;

	uint32_t intv;

	/* prepare for looking up entity object */
	SET_UID_LCP(&lc, OBJ_ENTITY, uid);
	lc.data[1].ptr = NULL;
	lc.data[2].ui = INFINITY - 1;

	/* get the array of the portal uid(s) */
	num_of_portal = (uint32_t)cache_lookup(&lc, NULL, cb_portal_uids);
	portal_uids = (uint32_t *)lc.data[1].ptr;
	*intval = lc.data[2].ui;

	/* prepare for looking up portal object(s) */
	SET_UID_LCP(&lc, OBJ_PORTAL, 0);
	lc.id[1] = ISNS_PORTAL_IP_ADDR_ATTR_ID;
	lc.id[2] = ISNS_ESI_PORT_ATTR_ID;
	FOR_EACH_OBJS(portal_uids, num_of_portal, uid, {
		if (uid != 0) {
			lc.data[0].ui = uid;
			intv = cache_lookup(&lc, NULL, cb_esi_portal);
			if (intv != 0) {
				p = new_esi_portal(uid,
				    (in6_addr_t *)lc.data[1].ip,
				    lc.data[0].ui, lc.data[2].ui);
				if (p != NULL) {
					p->next = list;
					list = p;
					if (*intval > intv) {
						*intval = intv;
					}
				}
			}
		}
	});

	/* free up the portal uid array */
	free(portal_uids);

	return (list);
}

/*
 * ****************************************************************************
 *
 * ev_add:
 *	Add an ESI event.
 *
 * ev	- the ESI event.
 * init	- 0: initialization time, otherwise not.
 * return - error code.
 *
 * ****************************************************************************
 */
static int
ev_add(
	ev_t *ev,
	int init
)
{
	uint32_t intval;
	esi_portal_t *p;

	double rnd;
	uint32_t t = 0;

	/* get the portal(s) which are registered for ESI monitoring */
	/* and the second interval for ESI or registration expiration */
	p = extract_esi_portal(ev->uid, &intval);
	ev->intval = intval;
	if (p != NULL) {
		ev->type = EV_ESI;
		ev->portal = p;
		/* avoid running everything at the same time */
		if (init != 0) {
			/* generate random number within range (0, 1] */
			rnd = (rand() + 1) / (double)(RAND_MAX + 1);
			t = (uint32_t)(intval * rnd);
		}
	} else {
		/* no portal is registered for ESI monitoring, make */
		/* an entry for entity registration expiration */
		ev->type = EV_REG_EXP;
		ev->portal = NULL;
		if (init != 0) {
			t = intval;
		}
	}

	/* schedule the event */
	return (el_add(ev, t, NULL));
}

/*
 * global functions.
 */

/*
 * ****************************************************************************
 *
 * sigalrm:
 *	The signal handler for SIGALRM, the ESI proc uses the SIGALRM
 *	for waking up to perform the client status inquery.
 *
 * sig	- the signal.
 *
 * ****************************************************************************
 */
/*ARGSUSED*/
void
sigalrm(
	int sig
)
{
	/* wake up the idle */
	(void) pthread_mutex_lock(&idl_mtx);
	wakeup = 1; /* wake up naturally */
	(void) pthread_cond_signal(&idl_cond);
	(void) pthread_mutex_unlock(&idl_mtx);
}

/*
 * ****************************************************************************
 *
 * esi_load:
 *	Load an ESI event from data store.
 *
 * uid	- the Entity object UID.
 * eid	- the Entity object name.
 * len	- the length of the name.
 * return - error code.
 *
 * ****************************************************************************
 */
int
esi_load(
	uint32_t uid,
	uchar_t *eid,
	uint32_t len
)
{
	int ec = 0;

	/* make a new event */
	ev_t *ev = ev_new(uid, eid, len);

	/* put the new event to the list */
	if (ev != NULL) {
		ev->next = ev_list;
		ev_list = ev;
	} else {
		ec = ISNS_RSP_INTERNAL_ERROR;
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * verify_esi_portal:
 *	Verify ESI port and add the ESI entries after the ESI are loaded.
 *
 * return - error code.
 *
 * ****************************************************************************
 */
int
verify_esi_portal(
)
{
	int ec = 0;

	ev_t *ev;

	/* add each event from the list */
	while (ev_list != NULL && ec == 0) {
		ev = ev_list;
		ev_list = ev->next;
		ev->next = NULL;
		ec = ev_add(ev, 1);
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * esi_add:
 *	Add a new ESI event when a new Entity is registered.
 *
 * uid	- the Entity object UID.
 * eid	- the Entity object name.
 * len	- the length of the name.
 * return - error code.
 *
 * ****************************************************************************
 */
int
esi_add(
	uint32_t uid,
	uchar_t *eid,
	uint32_t len
)
{
	int ec = 0;

	/* make a new event */
	ev_t *ev = ev_new(uid, eid, len);

	if (ev != NULL) {
		/* interrupt idle */
		ev->flags |= EV_FLAG_WAKEUP;
		ec = ev_add(ev, 0);
	} else {
		ec = ISNS_RSP_INTERNAL_ERROR;
	}

	return (ec);
}

/*
 * ****************************************************************************
 *
 * esi_remove:
 *	Remove an ESI event immediately.
 *
 * uid	- the Entity object UID.
 * return - always successful.
 *
 * ****************************************************************************
 */
int
esi_remove(
	uint32_t uid
)
{
	(void) el_remove(uid, 0, 0);

	return (0);
}

/*
 * ****************************************************************************
 *
 * esi_remove_obj:
 *	Update an ESI event when a Entity object or a Portal object is
 *	removed from server. If the object is being removed because of
 *	ESI failure, the ESI event will be removed with a pending time,
 *	otherwise, the ESI will be removed immediately.
 *
 * obj	- the object being removed.
 * pending - the pending flag.
 * return - always successful.
 *
 * ****************************************************************************
 */
int
esi_remove_obj(
	const isns_obj_t *obj,
	int pending
)
{
	uint32_t puid, uid;

	switch (obj->type) {
	case OBJ_PORTAL:
		puid = get_parent_uid(obj);
		uid = get_obj_uid(obj);
		break;
	case OBJ_ENTITY:
		puid = get_obj_uid(obj);
		uid = 0;
		break;
	default:
		puid = 0;
		break;
	}

	if (puid != 0) {
		(void) el_remove(puid, uid, pending);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * get_stopwatch:
 *	Get the stopwatch. It might need to signal the condition to
 *	wake up the idle so the stopwatch gets updated.
 *
 * flag	- wake up flag.
 * return - the stopwatch.
 *
 * ****************************************************************************
 */
uint32_t
get_stopwatch(
	int flag
)
{
	uint32_t t;

	/* not re-schedule, wake up idle */
	(void) pthread_mutex_lock(&idl_mtx);
	if (flag != 0) {
		wakeup = 2; /* wake up manually */
		(void) pthread_cond_signal(&idl_cond);
	} else {
		wakeup = 0; /* clear previous interruption */
	}
	(void) pthread_mutex_unlock(&idl_mtx);

	/* get most current time */
	(void) pthread_mutex_lock(&stw_mtx);
	t = stopwatch;
	(void) pthread_mutex_unlock(&stw_mtx);

	return (t);
}

/*
 * ****************************************************************************
 *
 * ev_intval:
 *	Get the time interval of an ESI event.
 *
 * p	- the ESI event.
 * return - the time interval.
 *
 * ****************************************************************************
 */
uint32_t
ev_intval(
	void *p
)
{
	return (((ev_t *)p)->intval);
}

/*
 * ****************************************************************************
 *
 * ev_match:
 *	Check the ESI event maching an Entity object.
 *
 * p	- the ESI event.
 * uid	- the Entity object UID.
 * return - 1: match, otherwise not.
 *
 * ****************************************************************************
 */
int
ev_match(
	void *p,
	uint32_t uid
)
{
	if (((ev_t *)p)->uid == uid) {
		return (1);
	} else {
		return (0);
	}
}

/*
 * ****************************************************************************
 *
 * ev_remove:
 *	Remove a portal or an ESI event. If all of ESI portal has been
 *	removed, the ESI event will be marked as removal pending, which
 *	will result in removing the Entity object after the pending time.
 *
 * p	- the ESI event.
 * portal_uid	- the Portal object UID.
 * flag	- 0: the ESI is currently in use, otherwise it is scheduled.
 * pending	- flag for the ESI removal pending.
 * return - 0: the ESI is physically removed, otherwise not.
 *
 * ****************************************************************************
 */
int
ev_remove(
	void *p,
	uint32_t portal_uid,
	int flag,
	int pending
)
{
	ev_t *ev = (ev_t *)p;
	esi_portal_t **pp, *portal;

	int has_portal = 0;
	int state;

	/* remove one portal only */
	if (portal_uid != 0) {
		pp = &ev->portal;
		portal = *pp;
		while (portal != NULL) {
			/* found the match portal */
			if (portal->ref == portal_uid) {
				/* mark it as removed */
				portal->ref = 0;
				if (flag != 0) {
					/* not in use, remove it physically */
					*pp = portal->next;
					portal->next = NULL;
					free_esi_portal(portal);
				} else {
					pp = &portal->next;
				}
			} else {
				/* one or more esi portals are available */
				if (portal->ref != 0) {
					has_portal = 1;
				}
				pp = &portal->next;
			}
			portal = *pp;
		}
	}

	/* no portal available */
	if (has_portal == 0) {
		state = (pending << 1) | flag;
		switch (state) {
		case 0x0:
			/* mark the event as removed */
			ev->flags |= EV_FLAG_REMOVE;
			isnslog(LOG_DEBUG, "ev_remove",
			    "%s [%d] is marked as removed.",
			    ev->type == EV_ESI ? "ESI" : "REG_EXP",
			    ev->uid);
			break;
		case 0x1:
			/* physically remove the event */
			ev_free(ev);
			break;
		case 0x2:
		case 0x3:
			/* mark the event as removal pending */
			isnslog(LOG_DEBUG, "ev_remove",
			    "%s [%d] is marked as removal pending.",
			    ev->type == EV_ESI ? "ESI" : "REG_EXP",
			    ev->uid);
			ev->flags |= EV_FLAG_REM_P1;
			has_portal = 1;
			break;
		default:
			break;
		}
	} else {
		isnslog(LOG_DEBUG, "ev_remove", "%s [%d] removed portal %d.",
		    ev->type == EV_ESI ? "ESI" : "REG_EXP",
		    ev->uid, portal_uid);
	}

	return (has_portal);
}

/*
 * ****************************************************************************
 *
 * ev_free:
 *	Free an ESI event.
 *
 * p	- the ESI event.
 *
 * ****************************************************************************
 */
void
ev_free(
	void *p
)
{
	ev_t *ev = (ev_t *)p;

	/* free up all of portals */
	free_esi_portal(ev->portal);

	isnslog(LOG_DEBUG, "ev_free",
	    "%s [%d] is physically removed.",
	    ev->type == EV_ESI ? "ESI" : "REG_EXP",
	    ev->uid);

	free(ev->eid);

	/* free the event */
	free(ev);
}

/*
 * ****************************************************************************
 *
 * evf_init:
 *	Check the initial flag of an ESI event.
 *
 * p	- the ESI event.
 * return - 0: not initial, otherwise yes.
 *
 * ****************************************************************************
 */
int
evf_init(
	void *p
)
{
	return (((ev_t *)p)->flags & EV_FLAG_INIT);
}

/*
 * ****************************************************************************
 *
 * evf_again:
 *	Check the again flag of an ESI event.
 *	(this flag might be eliminated and use the init flag.)
 *
 * p	- the ESI event.
 * return - 0: not again, otherwise yes.
 *
 * ****************************************************************************
 */
int
evf_again(
	void *p
)
{
	return (((ev_t *)p)->flags & EV_FLAG_AGAIN);
}

/*
 * ****************************************************************************
 *
 * evf_wakeup:
 *	Check the wakeup flag of an ESI event. The idle might need to
 *	wake up before the event is scheduled.
 *
 * p	- the ESI event.
 * return - 0: no wakeup, otherwise yes.
 *
 * ****************************************************************************
 */
int
evf_wakeup(
	void *p
)
{
	return (((ev_t *)p)->flags & EV_FLAG_WAKEUP);
}

/*
 * ****************************************************************************
 *
 * evf_rem:
 *	Check the removal flag of an ESI event. The ESI entry might be
 *	marked as removal.
 *
 * p	- the ESI event.
 * return - 0: not removed, otherwise yes.
 *
 * ****************************************************************************
 */
int
evf_rem(
	void *p
)
{
	return (((ev_t *)p)->flags & EV_FLAG_REMOVE);
}

/*
 * ****************************************************************************
 *
 * evf_rem_pending:
 *	Check the removal pending flag of an ESI event. The ESI entry
 *	might be marked as removal pending. If it is, we will switch the
 *	event type and change the time interval.
 *
 * p	- the ESI event.
 * return - 0: not removal pending, otherwise yes.
 *
 * ****************************************************************************
 */
int
evf_rem_pending(
	void *p
)
{
	ev_t *ev = (ev_t *)p;
	if ((ev->flags & EV_FLAG_REM_P) != 0) {
		if (ev->type != EV_REG_EXP) {
			isnslog(LOG_DEBUG, "ev_rem_pending",
			    "%s [%d] is changed to REG_EXP.",
			    ev->type == EV_ESI ? "ESI" : "REG_EXP",
			    ev->uid);
			ev->type = EV_REG_EXP;
			ev->intval *= 2; /* after 2 ESI interval */
		}
		return (1);
	}

	return (0);
}

/*
 * ****************************************************************************
 *
 * evf_zero:
 *	Reset the event flag.
 *
 * p	- the ESI event.
 *
 * ****************************************************************************
 */
void
evf_zero(
	void *p
)
{
	ev_t *ev = (ev_t *)p;

	/* not acutally clear it, need to set again flag */
	/* and keep the removal pending flag */
	ev->flags = EV_FLAG_AGAIN | (ev->flags & EV_FLAG_REM_P);
}

/*
 * ****************************************************************************
 *
 * evl_append:
 *	Append an ESI event to the list, the list contains all of
 *	ESI events which are being processed at present.
 *
 * p	- the ESI event.
 *
 * ****************************************************************************
 */
void
evl_append(
	void *p
)
{
	ev_t *ev;

	ev = (ev_t *)p;
	ev->next = ev_list;
	ev_list = ev;
}

/*
 * ****************************************************************************
 *
 * evl_strip:
 *	Strip off an ESI event from the list after the event is being
 *	processed, it will be scheduled in the scheduler.
 *
 * p	- the ESI event.
 *
 * ****************************************************************************
 */
void
evl_strip(
	void *p
)
{
	ev_t **evp = &ev_list;
	ev_t *ev = *evp;

	while (ev != NULL) {
		if (ev == p) {
			*evp = ev->next;
			break;
		}
		evp = &ev->next;
		ev = *evp;
	}
}

/*
 * ****************************************************************************
 *
 * evl_remove:
 *	Remove an ESI event or a portal of an ESI event from the event list.
 *
 * id1	- the Entity object UID.
 * id2	- the Portal object UID.
 * pending - the pending flag.
 * return - 1: found a match event, otherwise not.
 *
 * ****************************************************************************
 */
int
evl_remove(
	uint32_t id1,
	uint32_t id2,
	int pending
)
{
	ev_t *ev = ev_list;

	while (ev != NULL) {
		/* found it */
		if (ev_match(ev, id1) != 0) {
			/* lock the event */
			(void) pthread_mutex_lock(&ev->mtx);
			/* mark it as removed */
			(void) ev_remove(ev, id2, 0, pending);
			/* unlock the event */
			(void) pthread_mutex_unlock(&ev->mtx);
			/* tell caller removal is done */
			return (1);
		}
		ev = ev->next;
	}

	/* not found it */
	return (0);
}

#define	ALARM_MAX	(21427200)

/*
 * ****************************************************************************
 *
 * idle:
 *	Idle for certain amount of time or a wakeup signal is recieved.
 *
 * t	- the idle time.
 * return - the time that idle left.
 *
 * ****************************************************************************
 */
static int
idle(
	uint32_t t
)
{
	uint32_t t1, t2, t3 = 0;
	int idl_int = 0;

	/* hold the mutex for stopwatch update */
	(void) pthread_mutex_lock(&stw_mtx);

	do {
		if (t > ALARM_MAX) {
			t1 = ALARM_MAX;
		} else {
			t1 = t;
		}

		/* start alarm */
		(void) alarm(t1);

		/* hold the mutex for idle condition */
		(void) pthread_mutex_lock(&idl_mtx);

		/* wait on condition variable to wake up idle */
		while (wakeup == 0) {
			(void) pthread_cond_wait(&idl_cond, &idl_mtx);
		}
		if (wakeup == 2) {
			idl_int = 1;
		}
		/* clean wakeup flag */
		wakeup = 0;

		/* release the mutex for idle condition */
		(void) pthread_mutex_unlock(&idl_mtx);

		/* stop alarm */
		t2 = alarm(0);

		/* seconds actually slept */
		t3 += t1 - t2;
		t -= t3;
	} while (t > 0 && idl_int == 0);

	/* increate the stopwatch by the actually slept time */
	stopwatch += t3;

	/* release the mutex after stopwatch is updated */
	(void) pthread_mutex_unlock(&stw_mtx);

	/* return the amount of time which is not slept */
	return (t);
}

/*
 * ****************************************************************************
 *
 * ev_ex:
 *	Execute an event. To inquiry the client status or
 *	perform registration expiration.
 *
 * ev	- the event.
 *
 * ****************************************************************************
 */
static void
ev_ex(
	ev_t *ev
)
{
	pthread_t tid;

	switch (ev->type) {
	case EV_ESI:
		if (pthread_create(&tid, NULL,
		    esi_monitor, (void *)ev) != 0) {
			isnslog(LOG_DEBUG, "ev_ex", "pthread_create() failed.");
			/* reschedule for next occurence */
			(void) el_add(ev, 0, NULL);
		} else {
			/* increase the thread ref count */
			inc_thr_count();
		}
		break;
	case EV_REG_EXP:
		(void) queue_msg_set(sys_q, REG_EXP, (void *)ev);
		break;
	default:
		break;
	}
}

/*
 * ****************************************************************************
 *
 * esi_proc:
 *	ESI thread entry, which:
 *	1: fetch an event from schedule,
 *	2: idle for some time,
 *	3: execute the event or re-schedule it,
 *	4: repeat from step 1 before server is being shutdown.
 *
 * arg	- the thread argument.
 *
 * ****************************************************************************
 */
/*ARGSUSED*/
void *
esi_proc(
	void *arg
)
{
	uint32_t t, t1, pt;
	ev_t *ev;

	void *evp;

	while (time_to_exit == B_FALSE) {
		ev = (ev_t *)el_first(&pt);

		/* caculate the idle time */
		if (ev != NULL) {
			if (pt > stopwatch) {
				t = pt - stopwatch;
			} else {
				t = 0;
			}
		} else {
			t = INFINITY;
		}

		do {
			/* block for a certain amount of time */
			if (t > 0) {
				isnslog(LOG_DEBUG, "esi_proc",
				    "idle for %d seconds.", t);
				t1 = idle(t);
			} else {
				t1 = 0;
			}
			if (t1 > 0) {
				isnslog(LOG_DEBUG, "esi_proc",
				    "idle interrupted after idle for "
				    "%d seconds.", t - t1);
			}
			if (time_to_exit != B_FALSE) {
				ev = NULL; /* force break */
			} else if (ev != NULL) {
				if (t1 > 0) {
					/* not naturally waken up */
					/* reschedule current event */
					evp = NULL;
					(void) el_add(ev, pt, &evp);
					ev = (ev_t *)evp;
					t = t1;
				} else {
					/* excute */
					isnslog(LOG_DEBUG, "esi_proc",
					    "excute the cron job[%d].",
					    ev->uid);
					ev_ex(ev);
					ev = NULL;
				}
			}
		} while (ev != NULL);
	}

	return (NULL);
}

/*
 * ****************************************************************************
 *
 * esi_ping:
 *	Ping the client with the ESI retry threshold for status inquiry.
 *
 * so	- the socket descriptor.
 * pdu	- the ESI packet.
 * pl	- the length of packet.
 * return - 1: status inquired, otherwise not.
 *
 * ****************************************************************************
 */
static int
esi_ping(
	int so,
	isns_pdu_t *pdu,
	size_t pl
)
{
	int try_cnt = 0;
	isns_pdu_t *rsp = NULL;
	size_t rsp_sz;

	int alive = 0;

	do {
		if (isns_send_pdu(so, pdu, pl) == 0) {
			if (isns_rcv_pdu(so, &rsp, &rsp_sz,
			    ISNS_RCV_SHORT_TIMEOUT) > 0) {
#ifdef DEBUG
				dump_pdu1(rsp);
#endif
				alive = 1;
				break;
			}
		} else {
			/* retry after 1 second */
			(void) sleep(1);
		}
		try_cnt ++;
	} while (try_cnt < esi_threshold);

	if (rsp != NULL) {
		free(rsp);
	}

	return (alive);
}

/*
 * ****************************************************************************
 *
 * esi_monitor:
 *	Child thread for client status mornitoring.
 *
 * arg	- the ESI event.
 *
 * ****************************************************************************
 */
static void *
esi_monitor(
	void *arg
)
{
	ev_t *ev = (ev_t *)arg;

	esi_portal_t *p;
	int so;

	isns_pdu_t *pdu = NULL;
	size_t sz;
	size_t pl;
	size_t half;

	time_t t;

	int feedback;

	/* lock the event for esi monitoring */
	(void) pthread_mutex_lock(&ev->mtx);

	if (evf_rem(ev) != 0) {
		goto mon_done;
	} else if (evf_rem_pending(ev) != 0) {
		goto mon_done;
	}

	/* timestamp */
	t = time(NULL);

	/* allocate ESI PDU */
	if (pdu_reset_esi(&pdu, &pl, &sz) != 0 ||
	    pdu_add_tlv(&pdu, &pl, &sz,
	    ISNS_TIMESTAMP_ATTR_ID, 8, (void *)&t, 1) != 0 ||
	    pdu_add_tlv(&pdu, &pl, &sz,
	    ISNS_EID_ATTR_ID, ev->eid_len, (void *)ev->eid, 0) != 0) {
		/* no memory, will retry later */
		goto mon_done;
	}

	/* set pdu head */
	pdu->version = htons((uint16_t)ISNSP_VERSION);
	pdu->func_id = htons((uint16_t)ISNS_ESI);
	pdu->xid = htons(get_server_xid());

	/* keep the current lenght of the playload */
	half = pl;

	p = ev->portal;
	while (p != NULL) {
		if (p->ref != 0 &&
		    /* skip IPv6 portal */
		    p->sz != sizeof (in6_addr_t) &&
		    pdu_add_tlv(&pdu, &pl, &sz,
		    ISNS_PORTAL_IP_ADDR_ATTR_ID,
		    sizeof (in6_addr_t), (void *)p->ip6, 0) == 0 &&
		    pdu_add_tlv(&pdu, &pl, &sz,
		    ISNS_PORTAL_PORT_ATTR_ID,
		    4, (void *)p->port, 0) == 0) {
			/* connect once */
			so = connect_to(p->sz, p->ip4, p->ip6, p->esip);
			if (so != -1) {
				feedback = esi_ping(so, pdu, pl);
				(void) close(so);
				/* p->so = so; */
			} else {
				/* cannot connect, portal is dead */
				feedback = 0;
			}
			if (feedback == 0) {
				isnslog(LOG_DEBUG, "esi_monitor",
				    "ESI ping failed.");
				(void) queue_msg_set(sys_q, DEAD_PORTAL,
				    (void *)p->ref);
			} else {
				goto mon_done;
			}
		}
		pl = half;
		p = p->next;
	}

mon_done:
	/* unlock the event after esi monitoring is done */
	(void) pthread_mutex_unlock(&ev->mtx);

	/* clean up pdu */
	if (pdu != NULL) {
		free(pdu);
	}

	/* set reschedule flags */
	ev->flags |= EV_FLAG_WAKEUP;

	/* reschedule for next occurence */
	(void) el_add(ev, 0, NULL);

	/* decrease the thread ref count */
	dec_thr_count();

	return (NULL);
}

/*
 * ****************************************************************************
 *
 * portal_dies:
 *	Handles the dead portal that ESI detected.
 *
 * uid	- the Portal object UID.
 *
 * ****************************************************************************
 */
void
portal_dies(
	uint32_t uid
)
{
	int ec = 0;

	lookup_ctrl_t lc;

	/* prepare the lookup control for deregistration */
	SET_UID_LCP(&lc, OBJ_PORTAL, uid);

	/* lock the cache for object deregistration */
	(void) cache_lock_write();

	/* deregister the portal */
	ec = dereg_object(&lc, 1);

	/* unlock cache and sync with data store */
	(void) cache_unlock_sync(ec);
}

/*
 * ****************************************************************************
 *
 * portal_dies:
 *	Handles the Entity registration expiration.
 *
 * p	- the ESI event.
 *
 * ****************************************************************************
 */
void
reg_expiring(
	void *p
)
{
	int ec = 0;
	ev_t *ev = (ev_t *)p;
	lookup_ctrl_t lc;

	/* prepare the lookup control for deregistration */
	SET_UID_LCP(&lc, OBJ_ENTITY, ev->uid);

	/* lock the cache for object deregistration */
	(void) cache_lock_write();

	if (evf_rem(ev) == 0) {
		/* deregister the entity */
		ec = dereg_object(&lc, 0);

		/* unlock cache and sync with data store */
		ec = cache_unlock_sync(ec);

		if (ec == 0) {
			/* successfuk, mark ev as removed */
			ev->flags |= EV_FLAG_REMOVE;
		} else {
			/* failed, retry after 3 mintues */
			ev->intval = 3 * 60;
			isnslog(LOG_DEBUG, "reg_expiring",
			    "dereg failed, retry after 3 mintues.");
		}
	} else {
		/* ev is marked as removed, no need to dereg */
		(void) cache_unlock_nosync();
	}

	/* reschedule it for next occurence */
	(void) el_add(ev, 0, NULL);
}