-
Notifications
You must be signed in to change notification settings - Fork 30
/
stub_tai.c
1165 lines (1049 loc) · 37 KB
/
stub_tai.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
/**
* @file stub_tai.c
* @brief The main TAI interface routines
* @author Scott Emery <scotte@cumulusnetworks.com>
*
* @copyright Copyright (C) 2018 Cumulus Networks, Inc. All rights reserved
* @copyright Copyright (c) 2018 Nippon Telegraph and Telephone Corporation
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*
*/
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include "tai.h"
#include "taimetadata.h"
static tai_service_method_table_t adapter_host_fns;
static bool initialized = false;
#define STUB_NUM_MODULE 4
#define STUB_NUM_HOSTIF 2
#define STUB_NUM_NETIF 1
typedef struct _tai_attr_node_t {
tai_attribute_t attr;
struct _tai_attr_node_t *next;
} tai_attr_node_t;
typedef struct _stub_object_t {
tai_object_id_t oid;
tai_attr_node_t *head;
} stub_object_t;
typedef struct _stub_module_t {
stub_object_t module;
stub_object_t hostifs[STUB_NUM_HOSTIF];
stub_object_t netifs[STUB_NUM_NETIF];
} stub_module_t;
static stub_module_t g_modules[STUB_NUM_MODULE];
/*------------------------------------------------------------------------------
Utility Functions
------------------------------------------------------------------------------*/
#undef __TAI_MODULE__
#define __TAI_MODULE__ TAI_API_UNSPECIFIED
/** @brief The Stub TAI adapter uses the following format for object ids */
typedef struct _stub_object_id_t {
uint16_t value;
uint16_t module;
uint32_t :24;
uint8_t type;
} stub_object_id_t;
/**
* @brief Find an attribute in a list of attributes
*
* @param [in] attr_id The attribute ID to find
* @param [in] attr_count The number of attributes in the list
* @param [in] attr_list A list of attributes
*
* @return tai_attribute_value_t* A pointer to the attribute's value, or NULL if
* not found.
*/
const tai_attribute_value_t * find_attribute_in_list(
_In_ tai_attr_id_t attr_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
while (attr_count--) {
if (attr_list->id == attr_id) {
return &attr_list->value;
}
attr_list++;
}
return NULL;
}
/**
* @brief Convert a TAI status code to an indexed status code
*
* If the status code in 'err' is one of the codes for a list of attributes, the
* index value is added to that code. Otherwise the 'err' code is returned.
*
* @param err A TAI_STATUS_* code
* @param idx An index into a list of attributes
*
* @return tai_status_t
*/
tai_status_t convert_tai_error_to_list( _In_ tai_status_t err, _In_ uint32_t idx)
{
if (TAI_STATUS_IS_INVALID_ATTRIBUTE(err) ||
TAI_STATUS_IS_INVALID_ATTR_VALUE(err) ||
TAI_STATUS_IS_ATTR_NOT_IMPLEMENTED(err) ||
TAI_STATUS_IS_UNKNOWN_ATTRIBUTE(err) ||
TAI_STATUS_IS_ATTR_NOT_SUPPORTED(err)) {
return err + idx;
}
return err;
}
#define TAI_SYSLOG(lvl, ...) tai_syslog(__TAI_MODULE__, lvl, __VA_ARGS__)
#define TAI_SYSLOG_DEBUG(...) TAI_SYSLOG(TAI_LOG_LEVEL_DEBUG, __VA_ARGS__)
#define TAI_SYSLOG_INFO(...) TAI_SYSLOG(TAI_LOG_LEVEL_INFO, __VA_ARGS__)
#define TAI_SYSLOG_NOTICE(...) TAI_SYSLOG(TAI_LOG_LEVEL_NOTICE, __VA_ARGS__)
#define TAI_SYSLOG_WARN(...) TAI_SYSLOG(TAI_LOG_LEVEL_WARN, __VA_ARGS__)
#define TAI_SYSLOG_ERROR(...) TAI_SYSLOG(TAI_LOG_LEVEL_ERROR, __VA_ARGS__)
#define TAI_SYSLOG_CRITICAL(...) TAI_SYSLOG(TAI_LOG_LEVEL_CRITICAL, __VA_ARGS__)
/**
* @brief Given a TAI logging level, convert to a syslog level.
*/
static int tai_to_syslog_level[TAI_LOG_LEVEL_MAX] = {
[TAI_LOG_LEVEL_DEBUG] = LOG_DEBUG,
[TAI_LOG_LEVEL_INFO] = LOG_INFO,
[TAI_LOG_LEVEL_NOTICE] = LOG_NOTICE,
[TAI_LOG_LEVEL_WARN] = LOG_WARNING,
[TAI_LOG_LEVEL_ERROR] = LOG_ERR,
[TAI_LOG_LEVEL_CRITICAL] = LOG_CRIT
};
/**
* @brief Given a TAI module, retrieve the syslog level for that module. This is
* set by calling the #tai_log_set function. The default is WARNING.
*/
static int api_log_level[TAI_API_MAX] = {
[0 ... TAI_API_MAX-1] = LOG_WARNING
};
/**
* @brief Log a message to the syslog facility. The message may be filtered
* based on the TAI API's previously set logging level.
*
* @param [in] tai_api_id The TAI API logging this message
* @param [in] log_level The TAI message priority
* @param [in] format A printf-like format string
*/
void tai_syslog(_In_ tai_api_t tai_api_id, _In_ tai_log_level_t log_level,
_In_ const char *format, ...)
{
va_list arglist;
if ((TAI_API_UNSPECIFIED > tai_api_id) || (TAI_API_MAX <= tai_api_id)) {
tai_api_id = TAI_API_UNSPECIFIED;
}
if ((TAI_LOG_LEVEL_DEBUG > log_level) || (TAI_LOG_LEVEL_MAX <= log_level)) {
log_level = TAI_LOG_LEVEL_ERROR;
}
setlogmask(LOG_UPTO(api_log_level[tai_api_id]));
va_start(arglist, format);
vsyslog(tai_to_syslog_level[log_level], format, arglist);
va_end(arglist);
return;
}
/**
* @brief Generic helper function for setting an attribute
*
* @param [in] head head of tai_attr_node list
* @param [in] meta metadata of the attribute to set
* @param [in] attr attribute to set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t _set_attribute(tai_attr_node_t** const head, const tai_attr_metadata_t* const meta, const tai_attribute_t* const attr) {
tai_attr_node_t *p = *head, **update = head;
tai_status_t ret;
if ( p == NULL ) {
goto alloc;
}
do {
if ( p->attr.id == attr->id ) {
return tai_metadata_deepcopy_attr_value(meta, attr, &p->attr);
}
update = &p->next;
p = p->next;
} while ( p != NULL );
alloc:
p = (tai_attr_node_t*)calloc(1, sizeof(tai_attr_node_t));
ret = tai_metadata_alloc_attr_value(meta, &p->attr, NULL);
if ( ret != TAI_STATUS_SUCCESS ) {
return ret;
}
ret = tai_metadata_deepcopy_attr_value(meta, attr, &p->attr);
if ( ret != TAI_STATUS_SUCCESS ) {
return ret;
}
*update = p;
return TAI_STATUS_SUCCESS;
}
/**
* @brief Generic helper function for getting an attribute
*
* @param [in] head head of tai_attr_node list
* @param [in] meta metadata of the attribute to set
* @param [in,out] attr A pointer to the attribute to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t _get_attribute(const tai_attr_node_t* const head, const tai_attr_metadata_t* const meta, tai_attribute_t* const attr) {
const tai_attr_node_t* p = head;
if ( p == NULL ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
do {
if ( p->attr.id == attr->id ) {
return tai_metadata_deepcopy_attr_value(meta, &p->attr, attr);
}
p = p->next;
} while ( p != NULL );
return TAI_STATUS_ITEM_NOT_FOUND;
}
/*------------------------------------------------------------------------------
Host Interface Object Functions
------------------------------------------------------------------------------*/
#undef __TAI_MODULE__
#define __TAI_MODULE__ TAI_API_HOSTIF
/**
* @brief Retrieve the value of an attribute
*
* @param [in] host_interface_id The host interface ID handle
* @param [in,out] attr A pointer to the attribute to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_get_host_interface_attribute(
_In_ tai_object_id_t host_interface_id,
_Inout_ tai_attribute_t *attr)
{
stub_object_id_t id = *(stub_object_id_t*)&host_interface_id;
stub_object_t object;
const tai_attr_metadata_t* meta;
if ( id.type != TAI_OBJECT_TYPE_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.module >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = g_modules[id.module].hostifs[id.value];
if ( object.oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
TAI_SYSLOG_DEBUG("Retrieving host interface attribute: %d", attr->id);
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_HOSTIF, attr->id);
if ( meta == NULL ) {
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}
return _get_attribute(object.head, meta, attr);
}
/**
* @brief Retrieve a list of attribute values
*
* @param [in] host_interface_id The host interface ID handle
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in,out] attr_list A list of attributes to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_get_host_interface_attributes(
_In_ tai_object_id_t host_interface_id,
_In_ uint32_t attr_count,
_Inout_ tai_attribute_t *attr_list)
{
uint32_t idx;
tai_status_t ret;
for (idx = 0; idx < attr_count; idx++) {
ret = stub_get_host_interface_attribute(host_interface_id, attr_list++);
if (ret) {
return convert_tai_error_to_list(ret, idx);
}
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Set the value of an attribute
*
* @param [in] host_interface_id The host interface ID handle
* @param [in] attr A pointer to the attribute to be set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_set_host_interface_attribute(
_In_ tai_object_id_t host_interface_id,
_In_ const tai_attribute_t *attr)
{
stub_object_id_t id = *(stub_object_id_t*)&host_interface_id;
stub_object_t* object;
const tai_attr_metadata_t* meta;
if ( id.type != TAI_OBJECT_TYPE_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.module >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = &g_modules[id.module].hostifs[id.value];
if ( object->oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
TAI_SYSLOG_DEBUG("Setting host interface attribute: %d", attr->id);
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_HOSTIF, attr->id);
if ( meta == NULL ) {
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}
return _set_attribute(&object->head, meta, attr);
}
/**
* @brief Set the values from a list of attributes
*
* @param [in] host_interface_id The host interface ID handle
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in] attr_list A list of attributes to be set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_set_host_interface_attributes(
_In_ tai_object_id_t host_interface_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
uint32_t idx;
tai_status_t ret;
for (idx = 0; idx < attr_count; idx++) {
ret = stub_set_host_interface_attribute(host_interface_id, attr_list++);
if (ret) {
return convert_tai_error_to_list(ret, idx);
}
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Host interface initialization. After the call the capability
* attributes should be ready for retrieval via
* tai_get_host_interface_attribute().
*
* @param [out] host_interface_id Handle which identifies the host interface
* @param [in] module_id Handle which identifies the module on which the host
* interface exists
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in] attr_list A list of attributes to set during initialization
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_create_host_interface(
_Out_ tai_object_id_t *host_interface_id,
_In_ tai_object_id_t module_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
tai_status_t ret;
const tai_attribute_value_t * hostif_addr;
int i;
bool found = false;
stub_object_id_t id = { .type = TAI_OBJECT_TYPE_HOSTIF };
hostif_addr = find_attribute_in_list(TAI_HOST_INTERFACE_ATTR_INDEX, attr_count, attr_list);
if (NULL == hostif_addr) {
TAI_SYSLOG_ERROR("The required TAI_HOST_INTERFACE_ATTR_INDEX attribute was not provided");
return TAI_STATUS_MANDATORY_ATTRIBUTE_MISSING;
}
for ( i = 0; i < STUB_NUM_MODULE; i++ ) {
if ( g_modules[i].module.oid == module_id ) {
found = true;
break;
}
}
if ( !found ) {
TAI_SYSLOG_ERROR("failed to create hostif: module %x not found", module_id);
return TAI_STATUS_ITEM_NOT_FOUND;
}
if ( hostif_addr->u32 >= STUB_NUM_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( g_modules[i].hostifs[hostif_addr->u32].oid != 0 ) {
return TAI_STATUS_ITEM_ALREADY_EXISTS;
}
id.module = i;
id.value = hostif_addr->u32;
*host_interface_id = *(tai_object_id_t*)&id;
g_modules[i].hostifs[hostif_addr->u32].oid = *host_interface_id;
ret = stub_set_host_interface_attributes(*host_interface_id, attr_count, attr_list);
if (TAI_STATUS_SUCCESS != ret) {
TAI_SYSLOG_ERROR("Error setting host interface attributes");
return ret;
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Release all resources associated with previously created host
* interface
*
* @param [in] host_interface_id The host interface ID handle being removed
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_remove_host_interface(_In_ tai_object_id_t host_interface_id)
{
stub_object_id_t id = *(stub_object_id_t*)&host_interface_id;
stub_object_t* object;
tai_attr_node_t *p, *next;
const tai_attr_metadata_t* meta;
tai_status_t ret;
if ( id.type != TAI_OBJECT_TYPE_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.module >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_HOSTIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = &g_modules[id.module].hostifs[id.value];
if ( object->oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
p = object->head;
while ( p != NULL ) {
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_HOSTIF, p->attr.id);
ret = tai_metadata_free_attr_value(meta, &p->attr, NULL);
if ( ret != TAI_STATUS_SUCCESS ) {
return ret;
}
next = p->next;
free(p);
p = next;
}
object->oid = 0;
return TAI_STATUS_SUCCESS;
}
/**
* @brief The host interface functions. This structure is retrieved via the
* #tai_api_query function.
*/
tai_host_interface_api_t stub_host_interface_api = {
.create_host_interface = stub_create_host_interface,
.remove_host_interface = stub_remove_host_interface,
.set_host_interface_attribute = stub_set_host_interface_attribute,
.set_host_interface_attributes = stub_set_host_interface_attributes,
.get_host_interface_attribute = stub_get_host_interface_attribute,
.get_host_interface_attributes = stub_get_host_interface_attributes
};
/*------------------------------------------------------------------------------
Network Interface Object Functions
------------------------------------------------------------------------------*/
#undef __TAI_MODULE__
#define __TAI_MODULE__ TAI_API_NETWORKIF
/**
* @brief Retrieve the value of an attribute
*
* @param [in] network_interface_id The network interface ID handle
* @param [in,out] attr A pointer to the attribute to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_get_network_interface_attribute(
_In_ tai_object_id_t network_interface_id,
_Inout_ tai_attribute_t *attr)
{
stub_object_id_t id = *(stub_object_id_t*)&network_interface_id;
stub_object_t object;
const tai_attr_metadata_t* meta;
if ( id.type != TAI_OBJECT_TYPE_NETWORKIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.module >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_NETIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = g_modules[id.module].netifs[id.value];
if ( object.oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
TAI_SYSLOG_DEBUG("Retrieving network interface attribute: %d", attr->id);
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_NETWORKIF, attr->id);
if ( meta == NULL ) {
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}
return _get_attribute(object.head, meta, attr);
}
/**
* @brief Retrieve a list of attribute values
*
* @param [in] network_interface_id The network interface ID handle
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in,out] attr_list A list of attributes to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_get_network_interface_attributes(
_In_ tai_object_id_t network_interface_id,
_In_ uint32_t attr_count,
_Inout_ tai_attribute_t *attr_list)
{
uint32_t idx;
tai_status_t ret;
for (idx = 0; idx < attr_count; idx++) {
ret = stub_get_network_interface_attribute(network_interface_id, attr_list++);
if (ret) {
return convert_tai_error_to_list(ret, idx);
}
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Set the value of an attribute
*
* @param [in] network_interface_id The network interface ID handle
* @param [in] attr A pointer to the attribute to be set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_set_network_interface_attribute(
_In_ tai_object_id_t network_interface_id,
_In_ const tai_attribute_t *attr)
{
stub_object_id_t id = *(stub_object_id_t*)&network_interface_id;
stub_object_t *object;
const tai_attr_metadata_t* meta;
if ( id.type != TAI_OBJECT_TYPE_NETWORKIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.module >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_NETIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = &g_modules[id.module].netifs[id.value];
if ( object->oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
TAI_SYSLOG_DEBUG("Setting network interface attribute: %d", attr->id);
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_NETWORKIF, attr->id);
if ( meta == NULL ) {
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}
return _set_attribute(&object->head, meta, attr);
}
/**
* @brief Set the values from a list of attributes
*
* @param [in] network_interface_id The network interface ID handle
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in] attr_list A list of attributes to be set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_set_network_interface_attributes(
_In_ tai_object_id_t network_interface_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
uint32_t idx;
tai_status_t ret;
for (idx = 0; idx < attr_count; idx++) {
ret = stub_set_network_interface_attribute(network_interface_id, attr_list++);
if (ret) {
return convert_tai_error_to_list(ret, idx);
}
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Network interface initialization. After the call the capability
* attributes should be ready for retrieval via
* tai_get_network_interface_attribute().
*
* @param [out] network_interface_id Handle which identifies the network
* interface
* @param [in] module_id Module id on which the network interface exists
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in] attr_list A list of attributes to set during initialization
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_create_network_interface(
_Out_ tai_object_id_t *network_interface_id,
_In_ tai_object_id_t module_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
tai_status_t ret;
const tai_attribute_value_t * netif_addr;
int i;
bool found = false;
stub_object_id_t id = { .type = TAI_OBJECT_TYPE_NETWORKIF };
netif_addr = find_attribute_in_list(TAI_NETWORK_INTERFACE_ATTR_INDEX, attr_count, attr_list);
if (NULL == netif_addr) {
TAI_SYSLOG_ERROR("The required TAI_NETWORK_INTERFACE_ATTR_INDEX attribute was not provided");
return TAI_STATUS_MANDATORY_ATTRIBUTE_MISSING;
}
for ( i = 0; i < STUB_NUM_MODULE; i++ ) {
if ( g_modules[i].module.oid == module_id ) {
found = true;
break;
}
}
if ( !found ) {
TAI_SYSLOG_ERROR("failed to create netif: module %x not found", module_id);
return TAI_STATUS_ITEM_NOT_FOUND;
}
if ( netif_addr->u32 >= STUB_NUM_NETIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( g_modules[i].netifs[netif_addr->u32].oid != 0 ) {
return TAI_STATUS_ITEM_ALREADY_EXISTS;
}
id.module = i;
id.value = netif_addr->u32;
*network_interface_id = *(tai_object_id_t*)&id;
g_modules[i].netifs[netif_addr->u32].oid = *network_interface_id;
ret = stub_set_network_interface_attributes(*network_interface_id, attr_count, attr_list);
if (TAI_STATUS_SUCCESS != ret) {
TAI_SYSLOG_ERROR("Error setting network interface attributes");
return ret;
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Release all resources associated with previously created network
* interface
*
* @param [in] network_interface_id The network interface ID handle being
* removed
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_remove_network_interface(_In_ tai_object_id_t network_interface_id)
{
stub_object_id_t id = *(stub_object_id_t*)&network_interface_id;
stub_object_t* object;
tai_attr_node_t *p, *next;
const tai_attr_metadata_t* meta;
tai_status_t ret;
if ( id.type != TAI_OBJECT_TYPE_NETWORKIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.module >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_NETIF ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = &g_modules[id.module].netifs[id.value];
if ( object->oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
p = object->head;
while ( p != NULL ) {
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_NETWORKIF, p->attr.id);
ret = tai_metadata_free_attr_value(meta, &p->attr, NULL);
if ( ret != TAI_STATUS_SUCCESS ) {
return ret;
}
next = p->next;
free(p);
p = next;
}
object->oid = 0;
return TAI_STATUS_SUCCESS;
}
/**
* @brief The network interface functions. This structure is retrieved via the
* #tai_api_query function.
*/
tai_network_interface_api_t stub_network_interface_api = {
.create_network_interface = stub_create_network_interface,
.remove_network_interface = stub_remove_network_interface,
.set_network_interface_attribute = stub_set_network_interface_attribute,
.set_network_interface_attributes = stub_set_network_interface_attributes,
.get_network_interface_attribute = stub_get_network_interface_attribute,
.get_network_interface_attributes = stub_get_network_interface_attributes
};
/*------------------------------------------------------------------------------
Module Object Functions
------------------------------------------------------------------------------*/
#undef __TAI_MODULE__
#define __TAI_MODULE__ TAI_API_MODULE
/**
* @brief Retrieve the value of an attribute
*
* @param [in] module_id The module ID handle
* @param [in,out] attr A pointer to the attribute to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_get_module_attribute(
_In_ tai_object_id_t module_id,
_Inout_ tai_attribute_t *attr)
{
stub_object_id_t id = *(stub_object_id_t*)&module_id;
stub_object_t object;
const tai_attr_metadata_t* meta;
if ( id.type != TAI_OBJECT_TYPE_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = g_modules[id.value].module;
if ( object.oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
TAI_SYSLOG_DEBUG("Retrieving module attribute: %d", attr->id);
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_MODULE, attr->id);
if ( meta == NULL ) {
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}
switch (attr->id) {
case TAI_MODULE_ATTR_NUM_HOST_INTERFACES:
attr->value.u32 = STUB_NUM_HOSTIF;
return TAI_STATUS_SUCCESS;
case TAI_MODULE_ATTR_NUM_NETWORK_INTERFACES:
attr->value.u32 = STUB_NUM_NETIF;
return TAI_STATUS_SUCCESS;
}
return _get_attribute(object.head, meta, attr);
}
/**
* @brief Retrieve a list of attribute values
*
* @param [in] module_id The module ID handle
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in,out] attr_list A list of attributes to be retrieved
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_get_module_attributes(
_In_ tai_object_id_t module_id,
_In_ uint32_t attr_count,
_Inout_ tai_attribute_t *attr_list)
{
uint32_t idx;
tai_status_t ret;
for (idx = 0; idx < attr_count; idx++) {
ret = stub_get_module_attribute(module_id, attr_list++);
if (ret) {
return convert_tai_error_to_list(ret, idx);
}
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Set the value of an attribute
*
* @param [in] module_id The module ID handle
* @param [in] attr A pointer to the attribute to be set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_set_module_attribute(
_In_ tai_object_id_t module_id,
_In_ const tai_attribute_t *attr)
{
stub_object_id_t id = *(stub_object_id_t*)&module_id;
stub_object_t *object;
const tai_attr_metadata_t* meta = NULL;
if ( id.type != TAI_OBJECT_TYPE_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = &g_modules[id.value].module;
if ( object->oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
TAI_SYSLOG_DEBUG("Setting module attribute: %d", attr->id);
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_MODULE, attr->id);
if ( meta == NULL ) {
return TAI_STATUS_ATTR_NOT_SUPPORTED_0;
}
return _set_attribute(&object->head, meta, attr);
}
/**
* @brief Set the values from a list of attributes
*
* @param [in] module_id The module ID handle
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in] attr_list A list of attributes to be set
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_set_module_attributes(
_In_ tai_object_id_t module_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
uint32_t idx;
tai_status_t ret;
for (idx = 0; idx < attr_count; idx++) {
ret = stub_set_module_attribute(module_id, attr_list++);
if (ret) {
return convert_tai_error_to_list(ret, idx);
}
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Module initialization. After the call the capability attributes should
* be ready for retrieval via tai_get_module_attribute().
*
* @param [out] module_id Handle which identifies the module
* @param [in] attr_count A count of the number of elements in the attr_list
* @param [in] attr_list A list of attributes to set during initialization
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_create_module(
_Out_ tai_object_id_t *module_id,
_In_ uint32_t attr_count,
_In_ const tai_attribute_t *attr_list)
{
tai_status_t ret;
const tai_attribute_value_t * mod_addr;
stub_object_id_t id = { .type = TAI_OBJECT_TYPE_MODULE };
int i = -1;
mod_addr = find_attribute_in_list(TAI_MODULE_ATTR_LOCATION, attr_count, attr_list);
if (NULL == mod_addr) {
TAI_SYSLOG_ERROR("The required TAI_MODULE_ATTR_LOCATION attribute was not provided");
return TAI_STATUS_MANDATORY_ATTRIBUTE_MISSING;
}
sscanf(mod_addr->charlist.list, "%d", &i);
if ( i < 0 || i > STUB_NUM_MODULE ) {
TAI_SYSLOG_ERROR("Invalid module location: %s", mod_addr->charlist.list);
return TAI_STATUS_FAILURE;
}
id.value = i;
*module_id = *(tai_object_id_t*)&id;
g_modules[i].module.oid = *module_id;
ret = stub_set_module_attributes(*module_id, attr_count, attr_list);
if (TAI_STATUS_SUCCESS != ret) {
TAI_SYSLOG_ERROR("Error setting module attributes");
return ret;
}
return TAI_STATUS_SUCCESS;
}
/**
* @brief Release all resources associated with previously created module
*
* @param [in] module_id The module ID handle being removed
*
* @return TAI_STATUS_SUCCESS on success, failure status code on error
*/
static tai_status_t stub_remove_module(_In_ tai_object_id_t module_id)
{
stub_object_id_t id = *(stub_object_id_t*)&module_id;
stub_object_t *object;
tai_attr_node_t *p, *next;
const tai_attr_metadata_t* meta;
tai_status_t ret;
int i;
if ( id.type != TAI_OBJECT_TYPE_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
if ( id.value >= STUB_NUM_MODULE ) {
return TAI_STATUS_INVALID_PARAMETER;
}
object = &g_modules[id.value].module;
if ( object->oid == 0 ) {
return TAI_STATUS_ITEM_NOT_FOUND;
}
for ( i = 0; i < STUB_NUM_HOSTIF; i++ ) {
if ( g_modules[id.value].hostifs[i].oid != 0 ) {
return TAI_STATUS_OBJECT_IN_USE;
}
}
for ( i = 0; i < STUB_NUM_NETIF; i++ ) {
if ( g_modules[id.value].netifs[i].oid != 0 ) {
return TAI_STATUS_OBJECT_IN_USE;
}
}
p = object->head;
while ( p != NULL ) {
meta = tai_metadata_get_attr_metadata(TAI_OBJECT_TYPE_MODULE, p->attr.id);
ret = tai_metadata_free_attr_value(meta, &p->attr, NULL);
if ( ret != TAI_STATUS_SUCCESS ) {
return ret;
}
next = p->next;
free(p);
p = next;
}
object->oid = 0;
return TAI_STATUS_SUCCESS;
}
/**
* @brief The module interface functions. This structure is retrieved via the
* #tai_api_query function.
*/
tai_module_api_t stub_module_api = {
.create_module = stub_create_module,
.remove_module = stub_remove_module,
.set_module_attribute = stub_set_module_attribute,
.set_module_attributes = stub_set_module_attributes,
.get_module_attribute = stub_get_module_attribute,
.get_module_attributes = stub_get_module_attributes
};
/*------------------------------------------------------------------------------
TAI API Functions
------------------------------------------------------------------------------*/
/**
* @brief Adapter module initialization call. This is NOT for SDK
* initialization.
*
* @param [in] flags Reserved for future use, must be zero
* @param [in] services Methods table with services provided by adapter host