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
|
/*
* 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 (c) 2010, Oracle and/or its affiliates. All rights reserved.
*/
#include <arpa/inet.h>
#include <assert.h>
#include <libdllink.h>
#include <libdlstat.h>
#include <libnwam.h>
#include <libscf.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <values.h>
#include "conditions.h"
#include "events.h"
#include "objects.h"
#include "ncp.h"
#include "ncu.h"
#include "util.h"
/*
* ncp.c - handles NCP actions.
*/
char active_ncp[NWAM_MAX_NAME_LEN];
nwam_ncp_handle_t active_ncph = NULL;
int64_t current_ncu_priority_group = INVALID_PRIORITY_GROUP;
/*
* active_ncp_mutex protects active_ncp, active_ncph and
* current_ncu_priority_group.
*/
pthread_mutex_t active_ncp_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
* The variable ncu_wait_time specifies how long to wait to obtain a
* DHCP lease before giving up on that NCU and moving on to the next/lower
* priority-group.
*/
uint64_t ncu_wait_time = NCU_WAIT_TIME_DEFAULT;
/*
* Specifies if this is the first time the NCP has been enabled. True
* on startup so that we can differentiate between when we start up
* with a given NCP versus when we are asked to reenable it.
*/
boolean_t initial_ncp_enable = B_TRUE;
/*
* nwamd_ncp_handle_enable_event() should be called in the event handling
* loop in response to an _ENABLE event, triggered as a result of an
* nwam_ncp_enable() call from a libnwam consumer. To enable the new NCP,
* we first call nwamd_fini_ncus() on the old NCP. This results in enqueueing
* of a set of _FINI events for each NCU. These events are handled and in
* order to tear down config, (online*, uninitialized) state change events
* are created and consumed directly by the fini event handler (these events
* are not enqueued as this would result in state events for the old NCP
* appearing after the new NCP has been enabled. After the _FINI events are
* enqueued, we enqueue an NCP _OBJECT_STATE event for the new NCP. Since
* it is enqueued after the _FINI events, we are guaranteed no events for the
* old NCP will appear after the new NCP is activated.
*/
void
nwamd_ncp_handle_enable_event(nwamd_event_t event)
{
char *new_ncp = event->event_object;
nwam_ncp_handle_t new_ncph;
nwam_error_t err;
if (new_ncp[0] == '\0')
return;
(void) pthread_mutex_lock(&active_ncp_mutex);
if (strcmp(active_ncp, new_ncp) == 0 && !initial_ncp_enable) {
nlog(LOG_DEBUG, "nwamd_ncp_handle_enable_event: "
"%s is already active", new_ncp);
(void) pthread_mutex_unlock(&active_ncp_mutex);
return;
}
(void) pthread_mutex_unlock(&active_ncp_mutex);
nlog(LOG_DEBUG, "nwamd_ncp_handle_enable_event: activating NCP %s",
new_ncp);
/*
* To activate new NCP, run nwamd_fini_ncus(), reset the active
* priority-group, set the active_ncp property and refresh the
* daemon. The refresh action will trigger a re-read of the NCUs
* for the activated NCP.
*/
nwamd_fini_ncus();
if ((err = nwam_ncp_read(new_ncp, 0, &new_ncph))
== NWAM_ENTITY_NOT_FOUND) {
err = nwam_ncp_create(new_ncp, 0, &new_ncph);
}
if (err == NWAM_SUCCESS) {
nwam_ncp_free(new_ncph);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCP, new_ncp,
NWAM_STATE_ONLINE, NWAM_AUX_STATE_ACTIVE);
} else if (initial_ncp_enable) {
/*
* We weren't able to enable the NCP when nwamd starts up,
* retry in a few seconds.
*/
nwamd_event_t retry_event = nwamd_event_init_object_action
(NWAM_OBJECT_TYPE_NCP, new_ncp, NULL, NWAM_ACTION_ENABLE);
if (retry_event == NULL) {
nlog(LOG_ERR, "nwamd_ncp_handle_enable_event: "
"could not create retry event to enable %s NCP",
new_ncp);
return;
}
nlog(LOG_ERR, "nwamd_ncp_handle_enable_event: "
"failed to enable %s NCP, retrying in %d seconds",
new_ncp, NWAMD_READONLY_RETRY_INTERVAL);
nwamd_event_enqueue_timed(retry_event,
NWAMD_READONLY_RETRY_INTERVAL);
} else {
nlog(LOG_ERR, "nwamd_ncp_handle_enable_event: error %s",
nwam_strerror(err));
return;
}
}
void
nwamd_ncp_handle_action_event(nwamd_event_t event)
{
switch (event->event_msg->nwe_data.nwe_object_action.nwe_action) {
case NWAM_ACTION_ENABLE:
nwamd_ncp_handle_enable_event(event);
break;
case NWAM_ACTION_ADD:
case NWAM_ACTION_DESTROY:
/* nothing to do */
break;
default:
nlog(LOG_INFO, "nwam_ncp_handle_action_event: "
"unexpected action");
nwamd_event_do_not_send(event);
break;
}
}
/*
* The only state events we create are (online, active) events which are
* generated as part of an NCP enable action (see above).
*/
void
nwamd_ncp_handle_state_event(nwamd_event_t event)
{
char *new_ncp = event->event_object;
nwam_ncp_handle_t new_ncph, old_ncph;
nwam_error_t err;
/* The NCP to be activated should always exist. */
if ((err = nwam_ncp_read(new_ncp, 0, &new_ncph)) != NWAM_SUCCESS) {
nlog(LOG_ERR, "nwamd_ncp_handle_state_event: "
"cannot read NCP %s: : %s", new_ncp, nwam_strerror(err));
nwamd_event_do_not_send(event);
return;
}
/*
* To activate new NCP, reset the active priority-group, set the
* active_ncp property and refresh the daemon. The refresh action will
* trigger a re-read of the NCUs for the activated NCP.
*/
(void) pthread_mutex_lock(&active_ncp_mutex);
old_ncph = active_ncph;
active_ncph = new_ncph;
nwam_ncp_free(old_ncph);
current_ncu_priority_group = INVALID_PRIORITY_GROUP;
(void) strlcpy(active_ncp, event->event_object,
sizeof (active_ncp));
(void) pthread_mutex_unlock(&active_ncp_mutex);
(void) nwamd_set_string_property(OUR_FMRI, OUR_PG,
OUR_ACTIVE_NCP_PROP_NAME, new_ncp);
(void) smf_refresh_instance(OUR_FMRI);
initial_ncp_enable = B_FALSE;
}
int
nwamd_ncp_action(const char *ncp, nwam_action_t action)
{
nwamd_event_t event = nwamd_event_init_object_action
(NWAM_OBJECT_TYPE_NCP, ncp, NULL, action);
if (event == NULL)
return (1);
nwamd_event_enqueue(event);
return (0);
}
/*
* Below this point are routines handling NCU prioritization
* policy for the active NCP.
*/
struct priority_group_cbarg {
uint64_t minpriority;
uint64_t currpriority;
boolean_t found;
};
/* Callback used to find next pg in NCP that is >= start_pg */
static int
find_next_priority_group_cb(nwamd_object_t object, void *data)
{
struct priority_group_cbarg *cbarg = data;
uint64_t priority;
nwamd_ncu_t *ncu = object->nwamd_object_data;
if (ncu->ncu_link.nwamd_link_activation_mode !=
NWAM_ACTIVATION_MODE_PRIORITIZED)
return (0);
priority = ncu->ncu_link.nwamd_link_priority_group;
if (priority >= cbarg->minpriority && priority < cbarg->currpriority) {
cbarg->found = B_TRUE;
cbarg->currpriority = priority;
}
return (0);
}
/* Set current_pg to next pg in NCP that is >= start_pg */
boolean_t
nwamd_ncp_find_next_priority_group(int64_t minpriority,
int64_t *nextpriorityp)
{
struct priority_group_cbarg cbarg;
cbarg.minpriority = minpriority;
cbarg.currpriority = MAXINT;
cbarg.found = B_FALSE;
(void) nwamd_walk_objects(NWAM_OBJECT_TYPE_NCU,
find_next_priority_group_cb, &cbarg);
if (cbarg.found) {
nlog(LOG_DEBUG, "nwamd_ncp_find_next_priority_group: "
"next priority group >= %lld is %lld",
minpriority, cbarg.currpriority);
*nextpriorityp = cbarg.currpriority;
return (B_TRUE);
} else {
nlog(LOG_DEBUG, "nwamd_ncp_find_next_priority_group: "
"no priority groups >= %lld exist", minpriority);
return (B_FALSE);
}
}
/*
* Struct for walking NCUs in the selected priority group. We count
* how many of the exclusive, all and shared NCUs are online, and
* if activate_or_deactivate is true, we either activate or deactivate
* (depending on the value of activate) offline/online NCUs.
*/
struct nwamd_ncu_check_walk_arg {
boolean_t manual; /* enable manual NCUs only */
int64_t priority_group; /* interested priority-group for this walk */
uint64_t exclusive_ncus;
uint64_t exclusive_online_ncus;
uint64_t shared_ncus;
uint64_t shared_online_ncus;
uint64_t all_ncus;
uint64_t all_online_ncus;
boolean_t activate_or_deactivate;
boolean_t activate;
};
/*
* This function serves a number of purposes:
* - it supports activation/deactivation of manual NCUs in the current NCP
* (when wa->manual is true, wa->activate determines if we activate or
* deactivate the current NCU)
* - it supports checking/activation of a particular priority group in
* the active NCP. This works as follows:
*
* Count up numbers of exclusive, shared and all NCUs, and how many of each
* are online. If an NCU is waiting for IP address to be assigned, it is
* also considered online. If activate_or_deactivate is true, we also
* either activate (if activate is true) or deactivate prioritized NCUs
* that are offline or online.
*/
static int
nwamd_ncu_check_or_activate(nwamd_object_t object, void *data)
{
struct nwamd_ncu_check_walk_arg *wa = data;
nwamd_ncu_t *ncu;
uint64_t priority_group, priority_mode;
nwamd_object_t if_obj;
nwam_state_t state, if_state;
nwam_aux_state_t aux_state, if_aux_state;
char *name;
state = object->nwamd_object_state;
aux_state = object->nwamd_object_aux_state;
name = object->nwamd_object_name;
ncu = object->nwamd_object_data;
/* skip NCUs in UNINITIALIZED state */
if (state == NWAM_STATE_UNINITIALIZED) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"skipping uninitialized ncu %s", name);
return (0);
}
if (!wa->manual && wa->priority_group == INVALID_PRIORITY_GROUP)
return (0);
if (ncu->ncu_type != NWAM_NCU_TYPE_LINK) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"skipping interface NCU %s", name);
return (0);
}
if (!wa->manual && ncu->ncu_link.nwamd_link_activation_mode !=
NWAM_ACTIVATION_MODE_PRIORITIZED) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"skipping non-prioritized NCU %s", name);
return (0);
}
if (wa->manual && ncu->ncu_link.nwamd_link_activation_mode !=
NWAM_ACTIVATION_MODE_MANUAL) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"skipping non-manual NCU %s", name);
return (0);
}
priority_group = ncu->ncu_link.nwamd_link_priority_group;
priority_mode = ncu->ncu_link.nwamd_link_priority_mode;
/* Only work with NCUs in the requested priority-group */
if (!wa->manual && priority_group != wa->priority_group) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"skipping NCU %s in different priority-group", name);
return (0);
}
/* Get the state of the corresponding interface NCU */
if ((if_obj = nwamd_ncu_object_find(NWAM_NCU_TYPE_INTERFACE,
ncu->ncu_name)) == NULL) {
nlog(LOG_ERR, "nwamd_ncu_check_or_activate: "
"interface NCU of %s not found, skipping", name);
return (0);
}
if_state = if_obj->nwamd_object_state;
if_aux_state = if_obj->nwamd_object_aux_state;
nwamd_object_release(if_obj);
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: %s ncu %s",
wa->activate_or_deactivate ?
(wa->activate ? "activating" : "deactivating") :
"checking", name);
if (wa->manual) {
if (wa->activate_or_deactivate && wa->activate) {
if (state == NWAM_STATE_OFFLINE && ncu->ncu_enabled) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"moving NCU %s to offline* from offline",
name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_OFFLINE_TO_ONLINE,
NWAM_AUX_STATE_INITIALIZED);
}
if (state != NWAM_STATE_DISABLED &&
!ncu->ncu_enabled) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"moving NCU %s to online* (disabling)",
name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_ONLINE_TO_OFFLINE,
NWAM_AUX_STATE_MANUAL_DISABLE);
}
}
return (0);
}
switch (priority_mode) {
case NWAM_PRIORITY_MODE_EXCLUSIVE:
wa->exclusive_ncus++;
if (state == NWAM_STATE_ONLINE &&
(if_state == NWAM_STATE_ONLINE ||
if_aux_state == NWAM_AUX_STATE_IF_WAITING_FOR_ADDR))
wa->exclusive_online_ncus++;
/*
* For exclusive NCUs, we activate offline NCUs as long
* as no other exclusive NCUs are active.
*/
if (wa->activate_or_deactivate && wa->activate) {
if (state == NWAM_STATE_OFFLINE &&
wa->exclusive_online_ncus == 0) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"moving NCU %s to offline* from offline",
name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_OFFLINE_TO_ONLINE,
NWAM_AUX_STATE_INITIALIZED);
}
}
if (wa->activate_or_deactivate && !wa->activate) {
if (aux_state != NWAM_AUX_STATE_CONDITIONS_NOT_MET) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"deactivating NCU %s", name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_ONLINE_TO_OFFLINE,
NWAM_AUX_STATE_CONDITIONS_NOT_MET);
}
}
/*
* If we are activating or checking the priority group and
* too many exclusive NCUs are online, take this NCU down.
*/
if ((wa->activate_or_deactivate && wa->activate) ||
!wa->activate_or_deactivate) {
if (state == NWAM_STATE_ONLINE &&
if_state == NWAM_STATE_ONLINE &&
wa->exclusive_online_ncus > 1) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"moving NCU %s to online* since another "
"NCU is already active",
name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_ONLINE_TO_OFFLINE,
NWAM_AUX_STATE_CONDITIONS_NOT_MET);
}
}
break;
case NWAM_PRIORITY_MODE_SHARED:
wa->shared_ncus++;
if (state == NWAM_STATE_ONLINE &&
(if_state == NWAM_STATE_ONLINE ||
if_aux_state == NWAM_AUX_STATE_IF_WAITING_FOR_ADDR))
wa->shared_online_ncus++;
if (wa->activate_or_deactivate && wa->activate) {
if (state == NWAM_STATE_OFFLINE) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"activating NCU %s", name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_OFFLINE_TO_ONLINE,
NWAM_AUX_STATE_INITIALIZED);
}
}
if (wa->activate_or_deactivate && !wa->activate) {
if (aux_state != NWAM_AUX_STATE_CONDITIONS_NOT_MET) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"deactivating NCU %s", name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_ONLINE_TO_OFFLINE,
NWAM_AUX_STATE_CONDITIONS_NOT_MET);
}
}
break;
case NWAM_PRIORITY_MODE_ALL:
wa->all_ncus++;
if (state == NWAM_STATE_ONLINE &&
(if_state == NWAM_STATE_ONLINE ||
if_aux_state == NWAM_AUX_STATE_IF_WAITING_FOR_ADDR))
wa->all_online_ncus++;
/*
* For "all" NCUs, activate/deactivate all offline/online
* NCUs.
*/
if (wa->activate_or_deactivate && wa->activate) {
if (state == NWAM_STATE_OFFLINE) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"activating NCU %s", name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_OFFLINE_TO_ONLINE,
NWAM_AUX_STATE_INITIALIZED);
}
}
if (wa->activate_or_deactivate && !wa->activate) {
if (aux_state != NWAM_AUX_STATE_CONDITIONS_NOT_MET) {
nlog(LOG_DEBUG, "nwamd_ncu_check_or_activate: "
"deactivating NCU %s", name);
nwamd_object_set_state(NWAM_OBJECT_TYPE_NCU,
name, NWAM_STATE_ONLINE_TO_OFFLINE,
NWAM_AUX_STATE_CONDITIONS_NOT_MET);
}
}
break;
default:
nlog(LOG_ERR, "nwamd_ncu_check_or_activate: "
"invalid priority-mode");
break;
}
return (0);
}
void
nwamd_ncp_activate_priority_group(int64_t priority)
{
struct nwamd_ncu_check_walk_arg wa;
nwamd_event_t check_event, priority_event;
if (priority == INVALID_PRIORITY_GROUP)
return;
(void) pthread_mutex_lock(&active_ncp_mutex);
if (priority == current_ncu_priority_group) {
(void) pthread_mutex_unlock(&active_ncp_mutex);
return;
}
(void) pthread_mutex_unlock(&active_ncp_mutex);
nlog(LOG_DEBUG, "nwamd_ncp_activate_priority_group: "
"activating priority group %lld", priority);
wa.manual = B_FALSE;
wa.priority_group = priority;
wa.exclusive_ncus = 0;
wa.exclusive_online_ncus = 0;
wa.shared_ncus = 0;
wa.shared_online_ncus = 0;
wa.all_ncus = 0;
wa.all_online_ncus = 0;
wa.activate_or_deactivate = B_TRUE;
wa.activate = B_TRUE;
if (nwamd_walk_objects(NWAM_OBJECT_TYPE_NCU,
nwamd_ncu_check_or_activate, &wa) != 0) {
nlog(LOG_ERR, "nwamd_ncp_activate_priority_group: "
"nwamd_walk_objects() failed");
return;
}
/*
* Enqueue event to update current_ncu_priority_group and send to
* any event listeners.
*/
priority_event = nwamd_event_init_priority_group_change(priority);
if (priority_event == NULL)
return;
nwamd_event_enqueue(priority_event);
/*
* Now we've activated a new priority group, enqueue an event
* to check up on the state of this priority group.
*/
check_event = nwamd_event_init_ncu_check();
if (check_event == NULL)
return;
nwamd_event_enqueue_timed(check_event, ncu_wait_time);
}
void
nwamd_ncp_deactivate_priority_group(int64_t priority)
{
struct nwamd_ncu_check_walk_arg wa;
if (priority == INVALID_PRIORITY_GROUP)
return;
nlog(LOG_DEBUG, "nwamd_ncp_deactivate_priority_group: "
"deactivating priority group %lld", priority);
wa.manual = B_FALSE;
wa.priority_group = priority;
wa.exclusive_ncus = 0;
wa.exclusive_online_ncus = 0;
wa.shared_ncus = 0;
wa.shared_online_ncus = 0;
wa.all_ncus = 0;
wa.all_online_ncus = 0;
wa.activate_or_deactivate = B_TRUE;
wa.activate = B_FALSE;
if (nwamd_walk_objects(NWAM_OBJECT_TYPE_NCU,
nwamd_ncu_check_or_activate, &wa) != 0) {
nlog(LOG_ERR, "nwamd_ncp_deactivate_priority_group: "
"nwamd_walk_objects() failed");
return;
}
}
/*
* This function deactivates all priority groups at level 'priority' and lower
* (which is, numerically, all priorities >= priority).
*/
void
nwamd_ncp_deactivate_priority_group_all(int64_t priority)
{
if (priority == INVALID_PRIORITY_GROUP)
return;
nlog(LOG_DEBUG, "nwamd_ncp_deactivate_priority_group_all: "
"deactivating priority group less than or equal to %lld", priority);
do {
nwamd_ncp_deactivate_priority_group(priority);
} while (nwamd_ncp_find_next_priority_group(priority + 1, &priority));
}
/*
* Returns 'true' if it found the highest priority group no higher then what
* is passed that should be activated and sets *priority to that.
*/
boolean_t
nwamd_ncp_check_priority_group(int64_t *priority)
{
struct nwamd_ncu_check_walk_arg wa;
boolean_t conditions_met = B_FALSE;
nlog(LOG_DEBUG, "nwamd_ncp_check_priority_group: "
"checking priority group %lld", *priority);
if (*priority == INVALID_PRIORITY_GROUP) {
if (!nwamd_ncp_find_next_priority_group(0, priority))
return (B_FALSE);
}
while (!conditions_met) {
(void) memset(&wa, 0, sizeof (wa));
wa.manual = B_FALSE;
wa.priority_group = *priority;
wa.activate_or_deactivate = B_FALSE;
if (nwamd_walk_objects(NWAM_OBJECT_TYPE_NCU,
nwamd_ncu_check_or_activate, &wa) != 0) {
nlog(LOG_ERR, "nwamd_ncp_check_priority_group: "
"nwamd_walk_objects() failed");
return (B_FALSE);
}
/*
* Are activation conditons satisifed? In other words:
* - exactly one of the exclusive NCUs is online
* - 1 or more shared NCUs are online
* - all of the all NCUs are online.
* If any of these is untrue, conditions are not satisfied.
*/
conditions_met = B_TRUE;
if (wa.exclusive_ncus > 0 && wa.exclusive_online_ncus != 1)
conditions_met = B_FALSE;
if (wa.shared_ncus > 0 && wa.shared_online_ncus == 0)
conditions_met = B_FALSE;
if (wa.all_ncus > 0 && wa.all_ncus != wa.all_online_ncus)
conditions_met = B_FALSE;
if (wa.exclusive_online_ncus == 0 &&
wa.shared_online_ncus == 0 && wa.all_online_ncus == 0)
conditions_met = B_FALSE;
if (conditions_met) {
return (B_TRUE);
} else {
/*
* If there is a next pg, activate it. If not, do
* nothing - we're stuck here unless an event occurs
* for our or a higher pg.
*/
if (!nwamd_ncp_find_next_priority_group
(wa.priority_group + 1, priority)) {
nlog(LOG_DEBUG, "ran out of prio groups");
return (B_FALSE);
}
}
}
return (B_FALSE);
}
void
nwamd_ncp_activate_manual_ncus(void)
{
struct nwamd_ncu_check_walk_arg wa;
nlog(LOG_DEBUG, "nwamd_ncp_activate_manual_ncus: activating NCUs");
wa.manual = B_TRUE;
wa.activate_or_deactivate = B_TRUE;
wa.activate = B_TRUE;
if (nwamd_walk_objects(NWAM_OBJECT_TYPE_NCU,
nwamd_ncu_check_or_activate, &wa) != 0) {
nlog(LOG_ERR, "nwamd_ncp_activate_manual_ncus: "
"nwamd_walk_objects() failed");
return;
}
}
void
nwamd_create_ncu_check_event(uint64_t when)
{
nwamd_event_t check_event = nwamd_event_init_ncu_check();
if (check_event != NULL)
nwamd_event_enqueue_timed(check_event, when);
}
|