-
Notifications
You must be signed in to change notification settings - Fork 26
/
isnsdd.c
1168 lines (975 loc) · 28.3 KB
/
isnsdd.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
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
/*
* isnsdd - the iSNS Discovery Daemon
*
* Copyright (C) 2007 Olaf Kirch <[email protected]>
*
* The way isnsdd communicates with local services (initiator,
* target) is via a set of files and signals. That sounds rather
* awkward, but it's a lot simpler to add to these services
* than another socket based communication mechanism I guess.
*/
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>
#ifdef MTRACE
# include <mcheck.h>
#endif
#include <libisns/isns.h>
#include "config.h"
#include "security.h"
#include <libisns/util.h>
#include <libisns/isns-proto.h>
#include <libisns/paths.h>
#include <libisns/attrs.h>
enum {
ROLE_INITIATOR = 1,
ROLE_MONITOR = 2,
};
#define ISNSDD_REG_NAME "isns"
#define ISNSDD_PGT_OFFSET 10000
#define MAX_RETRY_TIMEOUT 300
typedef struct isns_proxy isns_proxy_t;
struct isns_proxy {
isns_list_t ip_list;
char * ip_eid;
isns_object_t * ip_entity;
isns_client_t * ip_client;
isns_object_list_t ip_objects;
time_t ip_last_registration;
};
static const char * opt_configfile = ISNS_DEFAULT_ISNSDD_CONFIG;
static int opt_af = AF_INET6;
static int opt_foreground = 0;
static int opt_role = ROLE_INITIATOR;
static int opt_scn_bits = ISNS_SCN_OBJECT_UPDATED_MASK |
ISNS_SCN_OBJECT_ADDED_MASK |
ISNS_SCN_OBJECT_REMOVED_MASK |
ISNS_SCN_TARGET_AND_SELF_ONLY_MASK;
static unsigned int opt_retry_timeout = 10;
static int opt_esi = 1;
static isns_socket_t * server_socket;
static ISNS_LIST_DECLARE(proxies);
static isns_object_list_t local_registry = ISNS_OBJECT_LIST_INIT;
static isns_object_list_t local_portals = ISNS_OBJECT_LIST_INIT;
static isns_object_list_t visible_nodes = ISNS_OBJECT_LIST_INIT;
static unsigned int esi_interval;
static int should_reexport;
static void run_discovery(isns_server_t *srv);
static void scn_callback(isns_db_t *, uint32_t,
isns_object_template_t *,
const char *, const char *);
static void refresh_registration(void *);
static void retry_registration(void *);
static void load_exported_objects(void);
static void store_imported_objects(void);
static void usage(int, const char *);
static void install_sighandler(int, void (*func)(int));
static void sig_cleanup(int);
static void sig_reread(int);
static struct option options[] = {
{ "config", required_argument, NULL, 'c' },
{ "debug", required_argument, NULL, 'd' },
{ "foreground", no_argument, NULL, 'f' },
{ "role", required_argument, NULL, 'r' },
{ "no-esi", no_argument, NULL, 'E' },
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'V' },
{ NULL }
};
int
main(int argc, char **argv)
{
isns_server_t *server;
isns_source_t *source;
isns_db_t *db;
int c;
#ifdef MTRACE
mtrace();
#endif
while ((c = getopt_long(argc, argv, "46c:d:Efhr:", options, NULL)) != -1) {
switch (c) {
case '4':
opt_af = AF_INET;
break;
case '6':
opt_af = AF_INET6;
break;
case 'c':
opt_configfile = optarg;
break;
case 'd':
isns_enable_debugging(optarg);
break;
case 'E':
opt_esi = 0;
break;
case 'f':
opt_foreground = 1;
break;
case 'h':
usage(0, NULL);
case 'r':
if (!strcasecmp(optarg, "initiator"))
opt_role = ROLE_INITIATOR;
else
if (!strcasecmp(optarg, "control")
|| !strcasecmp(optarg, "monitor"))
opt_role = ROLE_MONITOR;
else {
isns_error("Unknown role \"%s\"\n", optarg);
usage(1, NULL);
}
break;
case 'V':
printf("Open-iSNS version %s\n"
"Copyright (C) 2007, Olaf Kirch <[email protected]>\n",
OPENISNS_VERSION_STRING);
return 0;
default:
usage(1, "Unknown option");
}
}
if (optind != argc)
usage(1, NULL);
#if 0
/* If the config code derives the source name
* automatically, we want it to be distinct from
* any other source name (chosen by eg the iSCSI
* initiator). Adding a suffix of ":isns" is a
* somewhat lame attempt.
*/
isns_config.ic_source_suffix = "isns";
#endif
isns_config.ic_pidfile = ISNS_RUNDIR "/isnsdd.pid";
isns_read_config(opt_configfile);
if (!isns_config.ic_source_name) {
/*
* Try to read the source name from open-iscsi configuration
*/
isns_read_initiatorname(ISCSI_DEFAULT_INITIATORNAME);
}
isns_init_names();
if (!isns_config.ic_source_name)
usage(1, "Please specify an iSNS source name");
source = isns_source_create_iscsi(isns_config.ic_source_name);
isns_write_pidfile(isns_config.ic_pidfile);
if (!opt_foreground) {
if (daemon(0, 0) < 0)
isns_fatal("Unable to background server process\n");
isns_log_background();
isns_update_pidfile(isns_config.ic_pidfile);
}
install_sighandler(SIGTERM, sig_cleanup);
install_sighandler(SIGINT, sig_cleanup);
install_sighandler(SIGUSR2, sig_reread);
/* Create a DB object that shadows our portal list. This is for ESI -
* when an ESI comes in, the library will look up the portal in this
* database, and update its mtime. By checking the mtime at regular
* intervals, we can verify whether the server's ESIs actually
* reach us.
*/
db = isns_db_open_shadow(&local_portals);
server = isns_create_server(source, db, &isns_callback_service_ops);
isns_server_set_scn_callback(server, scn_callback);
run_discovery(server);
return 0;
}
void
usage(int exval, const char *msg)
{
if (msg)
fprintf(stderr, "Error: %s\n", msg);
fprintf(stderr,
"Usage: isnsdd [options]\n\n"
" --role <role> Specify role (one of initiator, control)\n"
" --config Specify alternative config fille\n"
" --foreground Do not put daemon in the background\n"
" --no-esi Do not try to register an portals for ESI status inquiries\n"
" --debug Enable debugging (list of debug flags)\n"
" --help Print this message\n"
);
exit(exval);
}
void
install_sighandler(int signo, void (*func)(int))
{
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = func;
sigaction(signo, &act, NULL);
}
void
sig_reread(int sig)
{
should_reexport = 1;
}
void
sig_cleanup(int sig)
{
isns_remove_pidfile(isns_config.ic_pidfile);
exit(1);
}
/*
* Proxy handling functions
*/
static isns_proxy_t *
isns_create_proxy(const char *eid)
{
isns_proxy_t *proxy;
proxy = calloc(1, sizeof(*proxy));
isns_list_init(&proxy->ip_list);
proxy->ip_eid = strdup(eid);
return proxy;
}
static isns_proxy_t *
__isns_proxy_find(isns_list_t *head, const char *eid)
{
isns_list_t *pos, *next;
isns_list_foreach(head, pos, next) {
isns_proxy_t *proxy = isns_list_item(isns_proxy_t, ip_list, pos);
if (!strcmp(proxy->ip_eid, eid))
return proxy;
}
return NULL;
}
static isns_proxy_t *
isns_proxy_by_entity(const isns_object_t *entity)
{
isns_list_t *pos, *next;
isns_list_foreach(&proxies, pos, next) {
isns_proxy_t *proxy = isns_list_item(isns_proxy_t, ip_list, pos);
if (proxy->ip_entity == entity)
return proxy;
}
return NULL;
}
static void
isns_proxy_erase(isns_proxy_t *proxy)
{
isns_object_list_destroy(&proxy->ip_objects);
if (proxy->ip_client) {
isns_client_destroy(proxy->ip_client);
proxy->ip_client = NULL;
}
if (proxy->ip_entity) {
isns_object_release(proxy->ip_entity);
proxy->ip_entity = NULL;
}
isns_cancel_timer(refresh_registration, proxy);
}
static void
isns_proxy_free(isns_proxy_t *proxy)
{
isns_proxy_erase(proxy);
isns_list_del(&proxy->ip_list);
free(&proxy->ip_eid);
free(proxy);
}
/*
* Force a re-registration of the whole object set.
*/
static void
force_reregistration(isns_proxy_t *proxy)
{
isns_cancel_timer(refresh_registration, proxy);
isns_add_oneshot_timer(0, retry_registration, proxy);
}
/*
* Refresh the registration by calling DevAttrQry
*/
static void
refresh_registration(void *ptr)
{
isns_proxy_t *proxy = ptr;
isns_client_t *clnt = proxy->ip_client;
isns_object_list_t objects = ISNS_OBJECT_LIST_INIT;
isns_attr_list_t query_key = ISNS_ATTR_LIST_INIT;
isns_simple_t *qry = NULL;
uint32_t status;
isns_debug_state("Refreshing registration for %s\n", proxy->ip_eid);
isns_attr_list_append_string(&query_key,
ISNS_TAG_ENTITY_IDENTIFIER,
proxy->ip_eid);
qry = isns_create_query(clnt, &query_key);
isns_attr_list_destroy(&query_key);
/* We should have an async call mechanism. If the server
* is wedged, we'll block here, unable to service any other
* functions.
*/
status = isns_simple_call(clnt->ic_socket, &qry);
if (status != ISNS_SUCCESS) {
isns_error("Query failed: %s\n", isns_strerror(status));
goto re_register;
}
status = isns_query_response_get_objects(qry, &objects);
isns_simple_free(qry);
if (status == ISNS_SUCCESS) {
if (objects.iol_count != 0)
return;
} else {
isns_error("Unable to parse query response\n");
}
re_register:
isns_warning("Lost registration, trying to re-register\n");
force_reregistration(proxy);
}
/*
* Check if all portals have seen ESI messages from the server
*/
static void
check_portal_registration(void *ptr)
{
isns_object_list_t bad_portals = ISNS_OBJECT_LIST_INIT;
unsigned int i, need_reregister = 0, good_portals = 0;
time_t now;
isns_debug_state("%s()\n", __FUNCTION__);
now = time(NULL);
for (i = 0; i < local_portals.iol_count; ++i) {
isns_object_t *obj = local_portals.iol_data[i];
isns_portal_info_t portal_info;
isns_proxy_t *proxy;
time_t last_modified;
uint32_t interval;
char *ps;
if (!isns_object_get_uint32(obj, ISNS_TAG_ESI_INTERVAL, &interval))
continue;
last_modified = isns_object_last_modified(obj);
if (last_modified + 2 * interval > now) {
good_portals++;
continue;
}
isns_portal_from_object(&portal_info,
ISNS_TAG_PORTAL_IP_ADDRESS,
ISNS_TAG_PORTAL_TCP_UDP_PORT,
obj);
ps = isns_portal_string(&portal_info),
isns_notice("Portal %s did not receive ESIs within %u seconds - "
"server may have lost us.\n",
ps, now - last_modified);
proxy = isns_proxy_by_entity(isns_object_get_entity(obj));
if (!proxy)
continue;
/* If we haven't received ANY ESIs, ever, the portal
* may be using a non-routable IP */
if (last_modified <= proxy->ip_last_registration)
isns_object_list_append(&bad_portals, obj);
force_reregistration(proxy);
need_reregister++;
}
for (i = 0; i < bad_portals.iol_count; ++i)
isns_object_list_remove(&local_portals, bad_portals.iol_data[i]);
isns_object_list_destroy(&bad_portals);
if (need_reregister && local_portals.iol_count == 0) {
/* Force a re-registration from scratch.
* This time without ESI.
*/
isns_notice("Suspiciously little ESI traffic - server may be broken\n");
isns_notice("Disabling ESI\n");
opt_esi = 0;
}
}
static void
setup_esi_watchdog(void)
{
unsigned int i;
isns_cancel_timer(check_portal_registration, NULL);
esi_interval = 0;
for (i = 0; i < local_portals.iol_count; ++i) {
isns_object_t *obj = local_portals.iol_data[i];
uint32_t interval;
/* should always succeed */
if (isns_object_get_uint32(obj, ISNS_TAG_ESI_INTERVAL, &interval))
continue;
if (!esi_interval || interval < esi_interval)
esi_interval = interval;
}
if (esi_interval) {
isns_debug_state("Setting up timer to check for ESI reachability\n");
isns_add_timer(esi_interval * 4 / 5,
check_portal_registration,
NULL);
}
}
static void
load_exported_objects(void)
{
isns_debug_state("Reading list of exported objects\n");
isns_object_list_destroy(&local_registry);
if (!isns_local_registry_load("!" ISNSDD_REG_NAME, 0, &local_registry)) {
isns_warning("Unable to obtain locally registered objects\n");
return;
}
}
static void
store_imported_objects(void)
{
if (!isns_local_registry_store(ISNSDD_REG_NAME, 0, &visible_nodes))
isns_warning("Unable to store discovered objects\n");
}
/*
* Given the DevAttrReg response, extract the entity ID we
* have been assigned.
*/
static int
extract_entity_id(isns_proxy_t *proxy, isns_simple_t *resp)
{
isns_object_list_t resp_objects = ISNS_OBJECT_LIST_INIT;
isns_object_t *entity = NULL;
int status;
unsigned int i;
status = isns_query_response_get_objects(resp, &resp_objects);
if (status) {
isns_error("Unable to extract object list from "
"registration response: %s\n",
isns_strerror(status), status);
goto out;
}
for (i = 0; i < resp_objects.iol_count; ++i) {
isns_object_t *obj = resp_objects.iol_data[i];
uint32_t interval;
if (!isns_object_is_entity(obj))
continue;
if (entity) {
isns_error("Server returns more than one entity "
"in registration response. What a weirdo.\n");
continue;
}
entity = obj;
if (!isns_object_get_uint32(obj,
ISNS_TAG_REGISTRATION_PERIOD,
&interval))
continue;
if (interval == 0) {
isns_error("Server returns a registration period of 0\n");
continue;
}
isns_debug_state("Setting up timer for registration refresh\n");
isns_add_timer(interval / 2,
refresh_registration,
proxy);
}
for (i = 0; i < resp_objects.iol_count; ++i) {
isns_attr_list_t key_attrs = ISNS_ATTR_LIST_INIT;
isns_object_t *obj = resp_objects.iol_data[i];
uint32_t interval;
if (!isns_object_is_portal(obj)
|| !isns_object_get_uint32(obj, ISNS_TAG_ESI_INTERVAL, &interval))
continue;
if (interval == 0) {
isns_error("Server returns an ESI interval of 0\n");
continue;
}
isns_object_get_key_attrs(obj, &key_attrs);
if (!(obj = isns_object_list_lookup(&proxy->ip_objects, NULL, &key_attrs))) {
isns_error("Server response includes a portal we never registered\n");
continue;
}
isns_object_set_uint32(obj, ISNS_TAG_ESI_INTERVAL, interval);
/* Server enabled ESI for this portal, so add it to
* the list of local portals we regularly check for
* incoming ESI messages. */
isns_object_list_append(&local_portals, obj);
}
proxy->ip_last_registration = time(NULL);
out:
isns_object_list_destroy(&resp_objects);
return status;
}
static inline void
__add_release_object(isns_object_list_t *objects, isns_object_t *cur)
{
if (cur == NULL)
return;
isns_object_list_append(objects, cur);
isns_object_release(cur);
}
/*
* Rebuild the list of proxies given the set of entities
*/
void
rebuild_proxy_list(isns_object_list_t *entities, isns_list_t *old_list)
{
isns_proxy_t *proxy;
unsigned int i;
isns_list_move(old_list, &proxies);
for (i = 0; i < entities->iol_count; ++i) {
isns_object_t *entity = entities->iol_data[i];
isns_object_t *node;
const char *eid;
eid = isns_entity_name(entity);
if (eid == NULL) {
isns_error("Whoopee, entity without name\n");
continue;
}
proxy = __isns_proxy_find(old_list, eid);
if (proxy == NULL) {
proxy = isns_create_proxy(eid);
} else {
isns_proxy_erase(proxy);
}
isns_object_list_append(&proxy->ip_objects, entity);
isns_object_get_descendants(entity, NULL, &proxy->ip_objects);
node = isns_object_list_lookup(&proxy->ip_objects,
&isns_iscsi_node_template,
NULL);
if (node == NULL) {
isns_warning("Service %s did not register any "
"storage nodes - skipped\n", eid);
continue;
}
proxy->ip_client = isns_create_client(NULL,
isns_storage_node_name(node));
proxy->ip_entity = isns_object_get(entity);
isns_list_del(&proxy->ip_list);
isns_list_append(&proxies, &proxy->ip_list);
}
}
/*
* Unregister old proxies
*/
static void
unregister_entities(isns_list_t *list)
{
while (!isns_list_empty(list)) {
isns_proxy_t *proxy = isns_list_item(isns_proxy_t, ip_list, list->next);
/* XXX send a DevDereg */
isns_proxy_free(proxy);
}
}
/*
* The local registry creates fake entities to group objects
* registered by the same service. We use this to perform
* several registration calls, each with a different EID
*/
static int
register_entity(isns_proxy_t *proxy)
{
isns_client_t *clnt = proxy->ip_client;
isns_simple_t *call = NULL;
int status;
call = isns_create_registration(clnt, proxy->ip_entity);
isns_registration_set_replace(call, 1);
isns_registration_add_object_list(call, &proxy->ip_objects);
status = isns_simple_call(clnt->ic_socket, &call);
if (status == ISNS_SUCCESS) {
/* Extract the EID and registration period */
extract_entity_id(proxy, call);
}
isns_simple_free(call);
return status;
}
static int
register_exported_entities(void)
{
int status = ISNS_SUCCESS;
isns_list_t *pos, *next;
isns_list_foreach(&proxies, pos, next) {
isns_proxy_t *proxy = isns_list_item(isns_proxy_t, ip_list, pos);
status = register_entity(proxy);
if (status != ISNS_SUCCESS)
break;
}
setup_esi_watchdog();
return status;
}
static void
all_objects_set(isns_object_list_t *list, uint32_t tag, uint32_t value)
{
unsigned int i;
for (i = 0; i < list->iol_count; ++i) {
isns_object_t *obj = list->iol_data[i];
isns_object_set_uint32(obj, tag, value);
}
}
static void
all_objects_unset(isns_object_list_t *list, uint32_t tag)
{
unsigned int i;
for (i = 0; i < list->iol_count; ++i) {
isns_object_t *obj = list->iol_data[i];
isns_object_delete_attr(obj, tag);
}
}
static int
register_exported_objects(isns_client_t *clnt)
{
isns_portal_info_t portal_info;
isns_object_list_t entities = ISNS_OBJECT_LIST_INIT;
isns_object_list_t portals = ISNS_OBJECT_LIST_INIT;
isns_simple_t *call = NULL;
int status, with_esi;
unsigned int i, my_port;
isns_list_t old_proxies;
if (!isns_socket_get_portal_info(server_socket, &portal_info))
isns_fatal("Unable to get portal info\n");
my_port = isns_portal_tcpudp_port(&portal_info);
/* Look up all entites and portals */
isns_object_list_gang_lookup(&local_registry,
&isns_entity_template, NULL,
&entities);
isns_object_list_gang_lookup(&local_registry,
&isns_portal_template, NULL,
&portals);
isns_list_init(&old_proxies);
rebuild_proxy_list(&entities, &old_proxies);
unregister_entities(&old_proxies);
/* Enable SCN on all portals we're about to register */
all_objects_set(&portals, ISNS_TAG_SCN_PORT, my_port);
/* Try ESI first. If the server doesn't support it, or doesn't
* have the resources to serve us, fall back to normal
* registration refresh. */
if (opt_esi) {
all_objects_set(&portals,
ISNS_TAG_ESI_INTERVAL,
isns_config.ic_esi_min_interval);
all_objects_set(&portals,
ISNS_TAG_ESI_PORT,
my_port);
}
for (with_esi = opt_esi; 1; with_esi--) {
status = register_exported_entities();
/* At some point, we need to add these portals
* to the local_portals list so that ESI works
* properly.
* Right now, we extract the portals from the response
* and add those. The down side of this is that we no
* longer use the same object (pointer) to refer to the
* same thing. The up side is that the information returned
* by the server reflects the correct ESI interval.
*/
if (status == ISNS_SUCCESS)
break;
if (status != ISNS_ESI_NOT_AVAILABLE || with_esi == 0) {
isns_error("Failed to register object(s): %s\n",
isns_strerror(status));
goto out;
}
/* Continue and retry without ESI */
all_objects_unset(&portals, ISNS_TAG_ESI_INTERVAL);
all_objects_unset(&portals, ISNS_TAG_ESI_PORT);
}
for (i = 0; i < local_registry.iol_count; ++i) {
isns_object_t *obj = local_registry.iol_data[i];
isns_source_t *source;
int status;
if (!isns_object_is_iscsi_node(obj)
&& !isns_object_is_fc_port(obj))
continue;
if (!(source = isns_source_from_object(obj)))
continue;
call = isns_create_scn_registration2(clnt, opt_scn_bits, source);
status = isns_simple_call(clnt->ic_socket, &call);
if (status != ISNS_SUCCESS) {
isns_error("SCN registration for %s failed: %s\n",
isns_storage_node_name(obj),
isns_strerror(status));
}
isns_source_release(source);
}
out:
if (call)
isns_simple_free(call);
isns_object_list_destroy(&entities);
isns_object_list_destroy(&portals);
return status;
}
static void
retry_registration(void *ptr)
{
isns_proxy_t *proxy = ptr;
static unsigned int timeout = 0;
int status;
status = register_exported_objects(proxy->ip_client);
if (status) {
if (timeout == 0)
timeout = opt_retry_timeout;
else if (timeout >= MAX_RETRY_TIMEOUT)
timeout = MAX_RETRY_TIMEOUT;
isns_debug_state("Retrying to register in %u seconds\n", timeout);
isns_add_oneshot_timer(timeout, retry_registration, proxy);
/* Exponential backoff */
timeout <<= 1;
}
}
/*
* Get a list of all visible storage nodes
*/
static int
get_objects_from_query(isns_simple_t *resp)
{
isns_object_list_t resp_objects = ISNS_OBJECT_LIST_INIT;
unsigned int i;
int status;
status = isns_query_response_get_objects(resp, &resp_objects);
if (status) {
isns_error("Unable to extract object list from "
"query response: %s\n",
isns_strerror(status));
return status;
}
isns_debug_state("Initial query returned %u object(s)\n", resp_objects.iol_count);
for (i = 0; i < resp_objects.iol_count; ++i) {
isns_attr_list_t key_attrs = ISNS_ATTR_LIST_INIT;
isns_object_t *obj = resp_objects.iol_data[i];
isns_object_t *found;
if (!isns_object_extract_keys(obj, &key_attrs))
continue;
/* Don't add an object twice, and don't add objects
* that *we* registered.
* This still leaves any default PGs created by the server,
* but we cannot help that (for now).
*/
found = isns_object_list_lookup(&visible_nodes, NULL, &key_attrs);
if (!found)
found = isns_object_list_lookup(&local_registry, NULL, &key_attrs);
if (found) {
isns_object_release(found);
} else {
isns_object_list_append(&visible_nodes, obj);
}
isns_attr_list_destroy(&key_attrs);
}
isns_object_list_destroy(&resp_objects);
return status;
}
static int
query_storage_node(isns_source_t *source, const char *name)
{
isns_attr_list_t key_attrs = ISNS_ATTR_LIST_INIT;
isns_simple_t *call;
uint32_t tag;
int status;
isns_client_t *clnt;
if (isns_source_type(source) != ISNS_TAG_ISCSI_NAME) {
isns_error("FC source node - doesn't work yet\n");
return ISNS_SUCCESS;
}
clnt = isns_create_client(NULL, isns_source_name(source));
tag = isns_source_type(source);
if (name) {
isns_attr_list_append_string(&key_attrs, tag, name);
} else {
/* Query for visible nodes */
isns_attr_list_append_nil(&key_attrs, tag);
}
call = isns_create_query2(clnt, &key_attrs, source);
isns_attr_list_destroy(&key_attrs);
isns_query_request_attr_tag(call, tag);
switch (tag) {
case ISNS_TAG_ISCSI_NAME:
isns_query_request_attr_tag(call, ISNS_TAG_ISCSI_NODE_TYPE);
isns_query_request_attr_tag(call, ISNS_TAG_ISCSI_ALIAS);
isns_query_request_attr_tag(call, ISNS_TAG_ISCSI_NODE_INDEX);
isns_query_request_attr_tag(call, ISNS_TAG_PORTAL_IP_ADDRESS);
isns_query_request_attr_tag(call, ISNS_TAG_PORTAL_TCP_UDP_PORT);
isns_query_request_attr_tag(call, ISNS_TAG_PORTAL_INDEX);
isns_query_request_attr_tag(call, ISNS_TAG_PG_ISCSI_NAME);
isns_query_request_attr_tag(call, ISNS_TAG_PG_PORTAL_IP_ADDR);
isns_query_request_attr_tag(call, ISNS_TAG_PG_PORTAL_TCP_UDP_PORT);
isns_query_request_attr_tag(call, ISNS_TAG_PG_TAG);
isns_query_request_attr_tag(call, ISNS_TAG_PG_INDEX);
break;
default: ;
}
status = isns_simple_call(clnt->ic_socket, &call);
if (status == ISNS_SUCCESS)
status = get_objects_from_query(call);
isns_simple_free(call);
isns_client_destroy(clnt);
return status;
}
/*
* Query for visible iscsi nodes
*/
static int
query_visible(void)
{
unsigned int i;
for (i = 0; i < local_registry.iol_count; ++i) {
isns_object_t *obj = local_registry.iol_data[i];
isns_source_t *source;
int status;
if (!isns_object_is_iscsi_node(obj)
&& !isns_object_is_fc_port(obj))
continue;
if (isns_object_is_fc_port(obj)) {
isns_error("FC source node - sorry, won't work yet\n");
continue;
}
if (!(source = isns_source_from_object(obj)))
continue;
status = query_storage_node(source, NULL);
if (status != ISNS_SUCCESS) {
isns_warning("Unable to run query on behalf of %s: %s\n",
isns_storage_node_name(obj),
isns_strerror(status));
}
isns_source_release(source);
}
return ISNS_SUCCESS;
}
/*
* Invoke the registered callout program
*/
static void
callout(const char *how, isns_object_t *obj, unsigned int bitmap)
{
char *argv[128];
int fargc, argc = 0;
pid_t pid;
if (!isns_config.ic_scn_callout)
return;
argv[argc++] = isns_config.ic_scn_callout;
argv[argc++] = (char *) how;
fargc = argc;
argc += isns_print_attrs(obj, argv + argc, 128 - argc);
pid = fork();
if (pid == 0) {
execv(argv[0], argv);
isns_fatal("Cannot execute %s: %m\n", argv[0]);
}
while (fargc < argc)
isns_free(argv[fargc++]);