aboutsummaryrefslogtreecommitdiffstats
path: root/ctrl/facemgr/src/facelet.c
blob: d33455b4d250a48d02886621b6a968d46f02e5b5 (plain)
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
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \file facelet.c
 * \brief Implementation of facelet
 */

#include <assert.h>
#include <stdbool.h>
#include <hicn/ctrl/face.h>
#include <hicn/ctrl/route.h>
#include <hicn/facemgr/cfg.h>
#include <hicn/facemgr/facelet.h>
#include <hicn/util/log.h>
#include <hicn/util/set.h>

#define FACELET_MAX_ROUTES 10

TYPEDEF_SET_H(route_set, hicn_route_t *);
TYPEDEF_SET(route_set, hicn_route_t *, hicn_route_cmp, generic_snprintf);

const char * face_type_layer_str[] = {
#define _(x) [FACE_TYPE_LAYER_ ## x] = STRINGIZE(x),
    foreach_face_type_layer
#undef _
};

const char * face_type_encap_str[] = {
#define _(x) [FACE_TYPE_ENCAP_ ## x] = STRINGIZE(x),
    foreach_face_type_encap
#undef _
};

const char * facelet_status_str[] = {
#define _(x) [FACELET_STATUS_ ## x] = STRINGIZE(x),
    foreach_facelet_status
#undef _
};

const char * facelet_error_reason_str[] = {
#define _(x) [FACELET_ERROR_REASON_ ## x] = STRINGIZE(x),
    foreach_facelet_error_reason
#undef _
};

/* Facelet attribute status */

const char * facelet_attr_status_str[] = {
#define _(x, str) [FACELET_ATTR_STATUS_ ## x] = STRINGIZE(x),
    foreach_facelet_attr_status
#undef _
};

const char * facelet_attr_status_str_short[] = {
#define _(x, str) [FACELET_ATTR_STATUS_ ## x] = STRINGIZE(str),
    foreach_facelet_attr_status
#undef _
};


/* Facelet */

struct facelet_s {
    unsigned id;

#define _(TYPE, NAME) TYPE NAME;
    foreach_facelet_attr
#undef _
#define _(TYPE, NAME) facelet_attr_status_t NAME ## _status;
    foreach_facelet_attr
#undef _



    facelet_status_t status;
    int error;

    facelet_event_t event;

    route_set_t * routes;
    bool routes_done;

    bool bj_done;
    bool au_done;
};

const char * facelet_event_str[] = {
#define _(x) [FACELET_EVENT_ ## x] = STRINGIZE(x),
foreach_facelet_event
#undef _
};

facelet_t *
facelet_create()
{
    facelet_t * facelet = calloc(1, sizeof(facelet_t));
    if (!facelet)
        goto ERR_MALLOC;

    facelet->id = 0;

    facelet->netdevice_status = FACELET_ATTR_STATUS_UNSET;
    facelet->netdevice_type_status = FACELET_ATTR_STATUS_UNSET;
    facelet->family_status = FACELET_ATTR_STATUS_UNSET;
    facelet->local_addr_status = FACELET_ATTR_STATUS_UNSET;
    facelet->local_port_status = FACELET_ATTR_STATUS_UNSET;
    facelet->remote_addr_status = FACELET_ATTR_STATUS_UNSET;
    facelet->remote_port_status = FACELET_ATTR_STATUS_UNSET;
    facelet->admin_state_status = FACELET_ATTR_STATUS_UNSET;
    facelet->state_status = FACELET_ATTR_STATUS_UNSET;
#ifdef WITH_POLICY
    facelet->priority_status = FACELET_ATTR_STATUS_UNSET;
#endif /* WITH_POLICY */
    facelet->face_type_status = FACELET_ATTR_STATUS_UNSET;

    facelet->status = FACELET_STATUS_UNDEFINED;
    facelet->error = 0;

    facelet->bj_done = false;
    facelet->au_done = false;

    facelet->event = FACELET_EVENT_UNDEFINED;

    facelet->routes = route_set_create();
    if (!facelet->routes) {
        ERROR("[facelet_create] Cannot create route set");
        goto ERR_ROUTE_SET;
    }
    facelet->routes_done = false;

    return facelet;

ERR_ROUTE_SET:
    free(facelet);
ERR_MALLOC:
    return NULL;
}

unsigned
facelet_get_id(facelet_t * facelet)
{
    return facelet->id;
}

void
facelet_set_id(facelet_t * facelet, unsigned id)
{
    facelet->id = id;
}

facelet_t *
facelet_create_from_netdevice(netdevice_t * netdevice)
{
    facelet_t * facelet = facelet_create();
    if (!facelet)
        goto ERR_FACELET;

    int rc = facelet_set_netdevice(facelet, *netdevice);
    if (rc < 0)
        goto ERR_NETDEV;

    return facelet;

ERR_NETDEV:
    facelet_free(facelet);
ERR_FACELET:
    return NULL;
}

/**
 * \brief Validate whether the facelet has all required fields to construct a
 *      face of the given type
 * \param [in] facelet - Pointer to the facelet to verify
 * \return 0 in case of success, -1 otherwise
 */
int
facelet_validate_face(const facelet_t * facelet)
{
    if (!facelet_has_face_type(facelet))
        return false;
    switch(facelet->face_type.layer) {
        case FACE_TYPE_LAYER_4:
            if (!facelet_has_remote_port(facelet))
                return false;
            if (!facelet_has_remote_addr(facelet))
                return false;
        case FACE_TYPE_LAYER_3:
            if (!facelet_has_local_port(facelet))
                return false;
            if (!facelet_has_local_addr(facelet))
                return false;
            if (!facelet_has_netdevice(facelet))
                return false;
            return true;

        default:
            return false; /* Error */
    }
    // FIXME Not implemented
    return 0;
}


netdevice_type_t
netdevice_type_from_face_tags(const face_t * face)
{
    policy_tags_t tags = face->tags;
    if (policy_tags_has(tags, POLICY_TAG_WIRED))
        return NETDEVICE_TYPE_WIRED;
    else if (policy_tags_has(tags, POLICY_TAG_WIFI))
        return NETDEVICE_TYPE_WIFI;
    else if (policy_tags_has(tags, POLICY_TAG_CELLULAR))
        return NETDEVICE_TYPE_CELLULAR;
    return NETDEVICE_TYPE_UNDEFINED;
}

facelet_t *
facelet_create_from_face(face_t * face)
{
    facelet_t * facelet = malloc(sizeof(facelet_t));
    if (!facelet)
        goto ERR_MALLOC;

    facelet->id = 0;

    /* Go through the face attributes to update the local representation */

    /* Attribute : netdevice */
    /* NOTE index is not set */
    if (IS_VALID_NETDEVICE(face->netdevice)) {
        /* /!\ A face has only the netdevice name */
        netdevice_set_name(&facelet->netdevice, face->netdevice.name);
        facelet->netdevice_status = FACELET_ATTR_STATUS_CLEAN;
    } else {
        facelet->netdevice_status = FACELET_ATTR_STATUS_UNSET;
    }

    /* Attribute : netdevice_type */
    facelet->netdevice_type = netdevice_type_from_face_tags(face);
    if (facelet->netdevice_type != NETDEVICE_TYPE_UNDEFINED) {
        facelet->netdevice_type_status = FACELET_ATTR_STATUS_CLEAN;
    } else {
        facelet->netdevice_type = NETDEVICE_TYPE_UNDEFINED;
        facelet->netdevice_type_status = FACELET_ATTR_STATUS_UNSET;
    }

    /* Attribute : family */
    if (IS_VALID_FAMILY(face->family)) {
        facelet->family = face->family;
        facelet->family_status = FACELET_ATTR_STATUS_CLEAN;

        /* Attribute : local_addr  */
        if (ip_address_cmp(&face->local_addr, &IP_ADDRESS_EMPTY, face->family) != 0) {
            facelet->local_addr = face->local_addr;
            facelet->local_addr_status = FACELET_ATTR_STATUS_CLEAN;
        } else {
            facelet->local_addr_status = FACELET_ATTR_STATUS_UNSET;
        }

        /* Attribute : local_port  */
        if (IS_VALID_PORT(face->local_port)) {
            facelet->local_port = face->local_port;
            facelet->local_port_status = FACELET_ATTR_STATUS_CLEAN;
        } else {
            facelet->local_port_status = FACELET_ATTR_STATUS_UNSET;
        }

        /* Attribute : remote_addr  */
        if (ip_address_cmp(&face->remote_addr, &IP_ADDRESS_EMPTY, face->family) != 0) {
            facelet->remote_addr = face->remote_addr;
            facelet->remote_addr_status = FACELET_ATTR_STATUS_CLEAN;
        } else {
            facelet->remote_addr_status = FACELET_ATTR_STATUS_UNSET;
        }

        /* Attribute : remote_port  */
        if (IS_VALID_PORT(face->remote_port)) {
            facelet->remote_port = face->remote_port;
            facelet->remote_port_status = FACELET_ATTR_STATUS_CLEAN;
        } else {
            facelet->remote_port_status = FACELET_ATTR_STATUS_UNSET;
        }

    } else {
        facelet->family_status = FACELET_ATTR_STATUS_UNSET;
        facelet->local_addr_status = FACELET_ATTR_STATUS_UNSET;
        facelet->local_port_status = FACELET_ATTR_STATUS_UNSET;
        facelet->remote_addr_status = FACELET_ATTR_STATUS_UNSET;
        facelet->remote_port_status = FACELET_ATTR_STATUS_UNSET;
    }

    /* Attribute : admin_state */
    if ((face->admin_state == FACE_STATE_UP) ||
            (face->admin_state == FACE_STATE_DOWN)) {
        facelet->admin_state = face->admin_state;
        facelet->admin_state_status = FACELET_ATTR_STATUS_CLEAN;
    } else {
        facelet->admin_state_status = FACELET_ATTR_STATUS_UNSET;
    }

    /* Attribute : state */
    if ((face->state == FACE_STATE_UP) ||
            (face->state == FACE_STATE_DOWN)) {
        facelet->state = face->state;
        facelet->state_status = FACELET_ATTR_STATUS_CLEAN;
    } else {
        facelet->state_status = FACELET_ATTR_STATUS_UNSET;
    }

#ifdef WITH_POLICY
    /* Attribute : priority */
    if (face->priority > 0) {
        facelet->priority = face->priority;
        facelet->priority_status = FACELET_ATTR_STATUS_CLEAN;
    } else {
        facelet->priority_status = FACELET_ATTR_STATUS_UNSET;
    }
#endif /* WITH_POLICY */

    /* Attribute : face_type */
    if ((face->type != FACE_TYPE_UNDEFINED) && (face->type != FACE_TYPE_N)) {
        switch(face->type) {
            case FACE_TYPE_UDP:
                facelet->face_type = FACEMGR_FACE_TYPE_OVERLAY_UDP;
                break;
            case FACE_TYPE_TCP:
                facelet->face_type = FACEMGR_FACE_TYPE_OVERLAY_TCP;
                break;
            case FACE_TYPE_HICN:
                facelet->face_type = FACEMGR_FACE_TYPE_NATIVE_TCP;
                break;
            default:
                ERROR("[facelet_create_from_face] Face type not (yet) implemented");
                goto ERR_FACE;
        }
        facelet->face_type_status = FACELET_ATTR_STATUS_CLEAN;
    } else {
        facelet->face_type_status = FACELET_ATTR_STATUS_UNSET;
    }

    /* Status */
    facelet->status = FACELET_STATUS_CLEAN;
    facelet->error = 0;

    /* TODO Consistency check between face type and found attributes */
    if (facelet_validate_face(facelet) < 0)
        goto ERR_FACE;

    facelet->bj_done = false;
    facelet->au_done = false;

    facelet->event = FACELET_EVENT_UNDEFINED;

    /* We need to get route set */
    facelet->routes = route_set_create();
    if (!facelet->routes) {
        ERROR("[facelet_create] Cannot create route set");
        goto ERR_ROUTE_SET;
    }
    facelet->routes_done = false;

    return facelet;

ERR_ROUTE_SET:
ERR_FACE:
    free(facelet);
ERR_MALLOC:
    return NULL;
}


void
facelet_free(facelet_t * facelet)
{
    /* Free up routes */
    route_set_clear(facelet->routes);
    route_set_free(facelet->routes);
    free(facelet);
}

facelet_t *
facelet_dup(const facelet_t * current_facelet)
{
    facelet_t * facelet = facelet_create();
    if (!facelet)
        goto ERR_CREATE;

#define _(TYPE, NAME) facelet-> NAME = current_facelet-> NAME;
    foreach_facelet_attr
#undef _
#define _(TYPE, NAME) facelet-> NAME ## _status = current_facelet-> NAME ## _status;
    foreach_facelet_attr
#undef _

    facelet->status = current_facelet->status;
    facelet->event = current_facelet->event;

    facelet->bj_done = current_facelet->bj_done;
    facelet->au_done = current_facelet->au_done;

    /* Routes */
    hicn_route_t ** route_array;
    int n = route_set_get_array(current_facelet->routes, &route_array);
    if (n < 0) {
        ERROR("[facelet_free] Error getting route set associated to facelet");
    } else {
        for (unsigned i = 0; i < n; i++) {
            hicn_route_t * route = route_array[i];
            hicn_route_t * new_route = hicn_route_dup(route);
            if (!new_route)
                goto ERR_ROUTE;
            route_set_add(facelet->routes, new_route);
        }
    }
    free(route_array);

    return facelet;

ERR_ROUTE:
    /* This will free all routes */
    facelet_free(facelet);
ERR_CREATE:
    return NULL;
}

int
facelet_cmp(const facelet_t * f1, const facelet_t * f2)
{
    /*
     * Under the assumption we only create a face per physical interface, a
     * facelet is uniquely identified by its netdevice attribute, and address
     * family if any.
     *
     * Because of additional static faces, we introduce a unique facelet id
     *
     * This function is mostly used for lookups into the cache, and the face
     * thus needs to have a netdevice associated, and optionally, an address
     * family.
     *
     * For other situations, the `facelet_match` function is more appropriate.
     */

    if (f1->id != f2->id)
        return f1->id > f2->id ?  1 : -1;

    if ((f1->netdevice_status != FACELET_ATTR_STATUS_UNSET) &&
            (f2->netdevice_status != FACELET_ATTR_STATUS_UNSET)) {
        int rc = netdevice_cmp(&f1->netdevice, &f2->netdevice);
        if (rc != 0)
            return rc;

    } else {
        /* Both unset : we might have the face without netdevice due to hicn
         * light not returning it currently, but we cannot skip it in the match
         * otherwise we cannot distinguish with other faces except matching on
         * other fields which might unfortunately not be determined yet...
         */
        return (f1->netdevice_status == FACELET_ATTR_STATUS_UNSET) ? -1 : 1;
    }

    assert(f1->family_status != FACELET_ATTR_STATUS_UNSET);
    assert(f2->family_status != FACELET_ATTR_STATUS_UNSET);

    if ((f1->family == AF_UNSPEC) || (f2->family == AF_UNSPEC))
        return 0;
    int diff = f1->family - f2->family;
    return (diff > 0) ? 1  :
           (diff < 0) ? -1 : 0;
}

/*
 * If the match has a field set, then the facelet only matches iif it has the
 * same field set, and both values are equal
 */
#define EQUALS_ATTRIBUTE(TYPE, NAME)                                            \
do {                                                                            \
    if (facelet_has_ ## NAME(facelet1)) {                                       \
        if (facelet_has_ ## NAME(facelet2)) {                                   \
            TYPE NAME ## 1;                                                     \
            TYPE NAME ## 2;                                                     \
            if (facelet_get_ ## NAME (facelet1, & NAME ## 1) < 0)               \
                return false;                                                   \
            if (facelet_get_ ## NAME (facelet2, & NAME ## 2) < 0)               \
                return false;                                                   \
            if (memcmp(& NAME ## 1, & NAME ## 2, sizeof(TYPE)) != 0)            \
                return false;                                                   \
        } else {                                                                \
            return false;                                                       \
        }                                                                       \
    } else {                                                                    \
        if (facelet_has_ ## NAME(facelet2)) {                                   \
            return false;                                                       \
        }                                                                       \
    }                                                                           \
} while(0)

bool
facelet_equals(const facelet_t * facelet1, const facelet_t * facelet2)
{
#define _(TYPE, NAME) EQUALS_ATTRIBUTE(TYPE, NAME);
    foreach_facelet_attr
#undef _
    return true;
}

/*
 * If the match has a field set, then the facelet only matches iif it has the
 * same field set, and both values are equal
 */
#define MATCH_ATTRIBUTE(TYPE, NAME)                                             \
do {                                                                            \
    if (facelet_match->NAME ## _status == FACELET_ATTR_STATUS_CLEAN) {          \
        if (facelet_has_ ## NAME(facelet_match)) {                              \
            TYPE NAME;                                                          \
            TYPE NAME ## _match;                                                \
            if (!facelet_has_ ## NAME(facelet)) {                               \
                continue; /* return false; */                                   \
            }                                                                   \
            if (facelet_get_ ## NAME (facelet, & NAME) < 0)                     \
                return false;                                                   \
            if (facelet_get_ ## NAME (facelet_match, & NAME ## _match) < 0)     \
                return false;                                                   \
            if (memcmp(& NAME, & NAME ## _match, sizeof(NAME)) != 0) {          \
                return false;                                                   \
            }                                                                   \
        }                                                                       \
    }                                                                           \
} while(0)

/* facelet_match is the incoming one */
bool
facelet_match(const facelet_t * facelet, const facelet_t * facelet_match)
{
#define _(TYPE, NAME) MATCH_ATTRIBUTE(TYPE, NAME);
    foreach_facelet_attr
#undef _

#if 0
    char facelet_s[MAXSZ_FACELET];
    facelet_snprintf(facelet_s, MAXSZ_FACELET, facelet_match);
    DEBUG("MATCHED FACELET %s", facelet_s);
    facelet_snprintf(facelet_s, MAXSZ_FACELET, facelet);
    DEBUG("         WITH %s", facelet_s);
#endif
    return true;
}

bool facelet_has_key(const facelet_t * facelet) {
    return (facelet_has_netdevice(facelet) && facelet_has_family(facelet));
}

/*
 * Implementation note:
 * - facelet_set_* is equivalent to merge with a CLEAN remote attribute
 */
#define FACELET_ACCESSORS(TYPE, NAME)                                           \
bool                                                                            \
facelet_has_ ## NAME(const facelet_t * facelet)                                 \
{                                                                               \
    assert(facelet);                                                            \
    assert(facelet->NAME ## _status != FACELET_ATTR_STATUS_UNDEFINED);          \
    assert(facelet->NAME ## _status != FACELET_ATTR_STATUS_N);                  \
    return ((facelet-> NAME ## _status != FACELET_ATTR_STATUS_UNSET));          \
}                                                                               \
                                                                                \
facelet_attr_status_t                                                           \
facelet_get_ ## NAME ## _status(const facelet_t * facelet)                      \
{                                                                               \
    return (facelet->NAME ## _status);                                          \
}                                                                               \
                                                                                \
void                                                                            \
facelet_set_ ## NAME ## _status(facelet_t * facelet,                            \
        facelet_attr_status_t status)                                           \
{                                                                               \
    facelet->NAME ## _status = status;                                          \
}                                                                               \
                                                                                \
int                                                                             \
facelet_get_ ## NAME(const facelet_t * facelet, TYPE * NAME)                    \
{                                                                               \
    assert(facelet);                                                            \
    if (!facelet_has_ ## NAME(facelet))                                         \
        return -1;                                                              \
    *NAME = facelet-> NAME;                                                     \
    return 0;                                                                   \
}                                                                               \
                                                                                \
int                                                                             \
facelet_set_local_ ## NAME(facelet_t * facelet, TYPE NAME)                      \
{                                                                               \
    assert(facelet);                                                            \
    switch(facelet->NAME ## _status) {                                          \
        case FACELET_ATTR_STATUS_UNSET:                                         \
        case FACELET_ATTR_STATUS_CLEAN:                                         \
        case FACELET_ATTR_STATUS_DIRTY:                                         \
        case FACELET_ATTR_STATUS_PENDING:                                       \
            facelet-> NAME = NAME;                                              \
            facelet->NAME ## _status = FACELET_ATTR_STATUS_DIRTY;               \
            if (facelet->status == FACELET_STATUS_CLEAN)                        \
                facelet->status = FACELET_STATUS_UPDATE;                        \
            break;                                                              \
        case FACELET_ATTR_STATUS_CONFLICT:                                      \
            break;                                                              \
        case FACELET_ATTR_STATUS_UNDEFINED:                                     \
        case FACELET_ATTR_STATUS_N:                                             \
            ERROR("Unexpected attribute status value");                         \
            return -1;                                                          \
    }                                                                           \
    return 0;                                                                   \
}                                                                               \
                                                                                \
int                                                                             \
facelet_set_remote_ ## NAME(facelet_t * facelet, TYPE NAME)                     \
{                                                                               \
    assert(facelet);                                                            \
    switch(facelet->NAME ## _status) {                                          \
        case FACELET_ATTR_STATUS_UNSET:                                         \
            facelet-> NAME = NAME;                                              \
            facelet->NAME ## _status = FACELET_ATTR_STATUS_CLEAN;               \
            break;                                                              \
        case FACELET_ATTR_STATUS_CLEAN:                                         \
            facelet->NAME = NAME;                                               \
            break;                                                              \
        case FACELET_ATTR_STATUS_DIRTY:                                         \
            ERROR("Discarded remote value for status reasons");                 \
            break;                                                              \
        case FACELET_ATTR_STATUS_PENDING:                                       \
            ERROR("Received remote value on pending attribute");                \
            facelet->NAME ## _status = FACELET_ATTR_STATUS_CONFLICT;            \
            /* We need to proceed to an update of the face */                   \
            if (facelet->status != FACELET_STATUS_UPDATE)                       \
                facelet->status = FACELET_STATUS_UPDATE;                        \
            break;                                                              \
        case FACELET_ATTR_STATUS_CONFLICT:                                      \
            return -1;                                                          \
        case FACELET_ATTR_STATUS_UNDEFINED:                                     \
        case FACELET_ATTR_STATUS_N:                                             \
            ERROR("Unexpected attribute status value");                         \
            return -1;                                                          \
    }                                                                           \
    return 0;                                                                   \
}                                                                               \
                                                                                \
int                                                                             \
facelet_set_ ## NAME(facelet_t * facelet, TYPE NAME)                            \
{                                                                               \
    return facelet_set_local_ ## NAME(facelet, NAME);                           \
}                                                                               \
                                                                                \
int                                                                             \
facelet_unset_ ## NAME(facelet_t * facelet)                                     \
{                                                                               \
    return facelet->NAME ## _status = FACELET_ATTR_STATUS_UNSET;                \
}

#define _(TYPE, NAME) FACELET_ACCESSORS(TYPE, NAME)
foreach_facelet_attr
#undef _

/*
 * This function is called for every facelet attribute. It is responsible for
 * comparing both the current and new value, and set the attribute and facelet
 * status appropriately.
 */

// FIXME CLEAN for key fields, dirty for fields to update.

#define MERGE_ATTRIBUTE(TYPE, NAME)                                                     \
do {                                                                                    \
    switch(facelet_to_merge->NAME ## _status) {                                         \
        case FACELET_ATTR_STATUS_UNDEFINED:                                             \
        case FACELET_ATTR_STATUS_N:                                                     \
        case FACELET_ATTR_STATUS_PENDING:                                               \
        case FACELET_ATTR_STATUS_CONFLICT:                                              \
            ERROR("Unexpected facelet attribute status");                               \
            return -1;                                                                  \
        case FACELET_ATTR_STATUS_UNSET:                                                 \
            break;                                                                      \
        case FACELET_ATTR_STATUS_CLEAN:                                                 \
        case FACELET_ATTR_STATUS_DIRTY:                                                 \
            facelet_set_ ## NAME(facelet, facelet_to_merge-> NAME);                     \
            break;                                                                      \
    }                                                                                   \
} while (0)

int facelet_merge(facelet_t * facelet, facelet_t * facelet_to_merge)
{
    assert(facelet && facelet_to_merge);

#define _(TYPE, NAME) MERGE_ATTRIBUTE(TYPE, NAME);
    foreach_facelet_attr
#undef _
    facelet->event = facelet_to_merge->event;

    /* Routes */
    hicn_route_t ** route_array;
    int n = route_set_get_array(facelet_to_merge->routes, &route_array);
    if (n < 0) {
        ERROR("[facelet_free] Error getting route set associated to facelet");
    } else {
        for (unsigned i = 0; i < n; i++) {
            hicn_route_t * route = route_array[i];
            hicn_route_t * route_found = NULL;
            if (route_set_get(facelet->routes, route, &route_found) < 0) {
                ERROR("Error searching for route");
                continue;
            }
            if (route_found)
                continue;
            route_set_add(facelet->routes, hicn_route_dup(route));
        }
    }
    free(route_array);

    return 0;
}

#define MERGE_ATTRIBUTE_REMOTE(TYPE, NAME)                                              \
do {                                                                                    \
    switch(facelet_to_merge->NAME ## _status) {                                         \
        case FACELET_ATTR_STATUS_UNDEFINED:                                             \
        case FACELET_ATTR_STATUS_N:                                                     \
        case FACELET_ATTR_STATUS_DIRTY:                                                 \
        case FACELET_ATTR_STATUS_PENDING:                                               \
        case FACELET_ATTR_STATUS_CONFLICT:                                              \
            ERROR("Unexpected facelet attribute status");                               \
            return -1;                                                                  \
        case FACELET_ATTR_STATUS_UNSET:                                                 \
            break;                                                                      \
        case FACELET_ATTR_STATUS_CLEAN:                                                 \
            facelet_set_ ## NAME(facelet, facelet_to_merge-> NAME);                     \
            break;                                                                      \
                                                                                        \
    }                                                                                   \
} while (0)

int facelet_merge_remote(facelet_t * facelet, const facelet_t * facelet_to_merge)
{
    assert(facelet && facelet_to_merge);
#define _(TYPE, NAME) MERGE_ATTRIBUTE_REMOTE(TYPE, NAME);
    foreach_facelet_attr
#undef _
    facelet->event = facelet_to_merge->event;
    return 0;
}

int
facelet_get_face(const facelet_t * facelet, face_t ** pface)
{
    assert(pface);

    /* Facelet has all the required information to create a face */
    if (facelet_validate_face(facelet) < 0) {
        ERROR("[facelet_get_face] Face does not validate");
        return 0;
    }

    face_t * face = face_create();
    if (!face)
        goto ERR_CREATE;

    assert(facelet_has_netdevice(facelet));
    face->netdevice = facelet->netdevice;

    /* Face type */
    switch(facelet->face_type.layer) {
        case FACE_TYPE_LAYER_4:
            switch(facelet->face_type.encap) {
                case FACE_TYPE_ENCAP_UDP:
                    face->type = FACE_TYPE_UDP;
                    break;
                case FACE_TYPE_ENCAP_TCP:
                    face->type = FACE_TYPE_TCP;
                    break;
                case FACE_TYPE_ENCAP_UNDEFINED:
                case FACE_TYPE_ENCAP_N:
                    ERROR("[facelet_get_face] Unsupported face encapsulation");
                    goto ERR;
            }

            if (facelet_get_family(facelet, &face->family) < 0) {
                ERROR("[facelet_get_face] Error retrieving face family");
                goto ERR;
            }
            if (facelet_get_local_addr(facelet, &face->local_addr) < 0) {
                ERROR("[facelet_get_face] Error retrieving face local address");
                goto ERR;
            }
            if (facelet_get_local_port(facelet, &face->local_port) < 0) {
                ERROR("[facelet_get_face] Error retrieving face local port");
                goto ERR;
            }
            if (facelet_get_remote_addr(facelet, &face->remote_addr) < 0) {
                ERROR("[facelet_get_face] Error retrieving face remote address");
                goto ERR;
            }
            if (facelet_get_remote_port(facelet, &face->remote_port) < 0) {
                ERROR("[facelet_get_face] Error retrieving face remote port");
                goto ERR;
            }
            break;

        case FACE_TYPE_LAYER_3:
            ERROR("{facelet_get_face] hICN face not (yet) implemented");
            goto ERR;

        case FACE_TYPE_LAYER_UNDEFINED:
        case FACE_TYPE_LAYER_N:
            ERROR("[facelet_get_face] Unsupported face type");
            goto ERR;
    }

    if (facelet_has_admin_state(facelet)) {
        if (facelet_get_admin_state(facelet, &face->admin_state) < 0) {
            ERROR("[facelet_get_face] Error getting face admin state");
            goto ERR;
        }
    } else {
        face->admin_state = FACE_STATE_UP;
    }

    if (facelet_has_state(facelet)) {
        if (facelet_get_state(facelet, &face->state) < 0) {
            ERROR("[facelet_get_face] Error getting face state");
            goto ERR;
        }
    } else {
        face->state = FACE_STATE_UP;
    }

#ifdef WITH_POLICY
    /* Priority */
    if (facelet_has_priority(facelet)) {
        if (facelet_get_priority(facelet, &face->priority) < 0) {
            ERROR("[facelet_get_face] Error getting face priority");
            goto ERR;
        }
    } else {
        face->priority = 0;
    }

    /* Tags */

    /* - based on netdevice type */
    policy_tags_t tags = POLICY_TAGS_EMPTY;
    if (facelet_has_netdevice_type(facelet)) {
        netdevice_type_t netdevice_type;
        if (facelet_get_netdevice_type(facelet, &netdevice_type) < 0) {
            ERROR("error getting netdevice_type");
            goto ERR;
        }


        switch(netdevice_type) {
            case NETDEVICE_TYPE_UNDEFINED:
            case NETDEVICE_TYPE_LOOPBACK:
                break;
            case NETDEVICE_TYPE_WIRED:
                policy_tags_add(&tags, POLICY_TAG_WIRED);
                break;
            case NETDEVICE_TYPE_WIFI:
                policy_tags_add(&tags, POLICY_TAG_WIFI);
                break;
            case NETDEVICE_TYPE_CELLULAR:
                policy_tags_add(&tags, POLICY_TAG_CELLULAR);
                break;
            default:
                goto ERR;
        }
    }
    face->tags = tags;
#endif /* WITH_POLICY */

    *pface = face;

    return 0;

ERR:
    free(face);
ERR_CREATE:
    *pface = NULL;
    return -1;
}

facelet_status_t
facelet_get_status(const facelet_t * facelet)
{
    return facelet->status;
}

void
facelet_set_status(facelet_t * facelet, facelet_status_t status)
{
    facelet->status = status;
}

#define SET_ATTR_STATUS_CLEAN(TYPE, NAME)                       \
do {                                                            \
    if (facelet->NAME ## _status  == FACELET_ATTR_STATUS_DIRTY) \
        facelet->NAME ## _status = FACELET_ATTR_STATUS_CLEAN;   \
} while (0)

void
facelet_set_attr_clean(facelet_t * facelet)
{
#define _(TYPE, NAME) SET_ATTR_STATUS_CLEAN(TYPE, NAME);
    foreach_facelet_attr
#undef _
}

void
facelet_set_error(facelet_t * facelet, facelet_error_reason_t reason)
{
    facelet->error++;
    switch(reason) {
        case FACELET_ERROR_REASON_UNSPECIFIED_ERROR:
        case FACELET_ERROR_REASON_INTERNAL_ERROR:
        case FACELET_ERROR_REASON_PERMISSION_DENIED:
            if (facelet->error >= FACELET_MAX_ERRORS)
                facelet_set_status(facelet, FACELET_STATUS_IGNORED);
            break;
        case FACELET_ERROR_REASON_FORWARDER_OFFLINE:
            break;
        case FACELET_ERROR_REASON_UNDEFINED:
        case FACELET_ERROR_REASON_N:
            ERROR("facelet_set_error] Unexpected error reason");
            break;
    }
}

void
facelet_unset_error(facelet_t * facelet)
{
    facelet->error = 0;
}

bool
facelet_get_error(const facelet_t * facelet)
{
    return facelet->error;
}

void
facelet_set_bj_done(facelet_t * facelet)
{
    facelet->bj_done = true;
}

void
facelet_unset_bj_done(facelet_t * facelet)
{
    facelet->bj_done = false;
}

bool
facelet_is_bj_done(const facelet_t * facelet)
{
    return facelet->bj_done;
}

void
facelet_set_au_done(facelet_t * facelet)
{
    facelet->au_done = true;
}

bool
facelet_is_au_done(const facelet_t * facelet)
{
    return facelet->au_done;
}

facelet_event_t
facelet_get_event(const facelet_t * facelet)
{
    return facelet->event;
}

void
facelet_set_event(facelet_t * facelet, facelet_event_t event)
{
    facelet->event = event;
}

int
facelet_add_route(facelet_t * facelet, hicn_route_t * route)
{
    return route_set_add(facelet->routes, route);
}

int
facelet_remove_route(facelet_t * facelet, hicn_route_t * route, hicn_route_t ** route_removed)
{
    return route_set_remove(facelet->routes, route, route_removed);
}

int
facelet_clear_routes(facelet_t * facelet)
{
    return route_set_clear(facelet->routes);
}

int
facelet_get_route_array(const facelet_t * facelet, hicn_route_t *** route_array)
{
    return route_set_get_array(facelet->routes, route_array);
}

int
facelet_snprintf(char * s, size_t size, const facelet_t * facelet)
{
    char * cur = s;
    int rc;

    assert(facelet);

    /* Header + key attributes (netdevice + family) */
    rc = snprintf(cur, s + size - cur, "<Facelet [%d] %s %s (%s)",
            facelet->id,
            facelet_status_str[facelet->status],
            facelet_get_error(facelet) ? "/!\\" : "",
            (facelet->family == AF_INET) ? "AF_INET" :
            (facelet->family == AF_INET6) ? "AF_INET6" :
            (facelet->family == AF_UNSPEC) ? "AF_UNSPEC" :
            "unknown");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    /* Netdevice */
    if (facelet_has_netdevice(facelet)) {
        rc = snprintf(cur, s + size - cur, " netdevice=%s",
                facelet->netdevice.name[0] ? facelet->netdevice.name : "*");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = snprintf(cur, s + size - cur, "/%d", facelet->netdevice.index);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

    } else {
        rc = snprintf(cur, s + size - cur, " netdevice=*/*");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Netdevice type */
    if (facelet_has_netdevice_type(facelet)) {
        rc = snprintf(cur, s + size - cur, " type=%s",
                netdevice_type_str[facelet->netdevice_type]);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Local ip address */
    if (facelet_has_local_addr(facelet)) {
        rc = snprintf(cur, s + size - cur, " local_addr=");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = ip_address_snprintf(cur, s + size - cur, &facelet->local_addr,
                facelet->family);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Local port */
    if (facelet_has_local_port(facelet)) {
        rc = snprintf(cur, s + size - cur, " local_port=%d",
                facelet->local_port);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Remote ip address */
    if (facelet_has_remote_addr(facelet)) {
        rc = snprintf(cur, s + size - cur, " remote_addr=");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = ip_address_snprintf(cur, s + size - cur, &facelet->remote_addr,
                facelet->family);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Remote port */
    if (facelet_has_remote_port(facelet)) {
        rc = snprintf(cur, s + size - cur, " remote_port=%d",
                facelet->remote_port);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Admin state */
    if (facelet_has_admin_state(facelet)) {
        rc = snprintf(cur, s + size - cur, " admin_state=%s",
                face_state_str[facelet->admin_state]);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* State */
    if (facelet_has_state(facelet)) {
        rc = snprintf(cur, s + size - cur, " state=%s",
                face_state_str[facelet->state]);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

#ifdef WITH_POLICY
    /* Priority */
    if (facelet_has_priority(facelet)) {
        rc = snprintf(cur, s + size - cur, " priority=%d", facelet->priority);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }
#endif /* WITH_POLICY */

    /* Face type */
    if (facelet_has_face_type(facelet)) {
        rc = snprintf(cur, s + size - cur, " face_type=LAYER%s/%s",
            FACEMGR_FACE_TYPE_STR(facelet->face_type));
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Routes */
    rc = snprintf(cur, s + size - cur, " routes={ ");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    hicn_route_t ** route_array;
    int n = route_set_get_array(facelet->routes, &route_array);
    if (n < 0) {
        ERROR("[facelet_free] Error getting route set associated to facelet");
    } else {
        for (unsigned i = 0; i < n; i++) {
            hicn_route_t * route = route_array[i];
            rc = hicn_route_snprintf(cur, s + size - cur, route);
            if (rc < 0)
                return rc;
            cur += rc;
            if (cur >= s + size)
                return cur - s;

            rc = snprintf(cur, s + size - cur, ", ");
            if (rc < 0)
                return rc;
            cur += rc;
            if (cur >= s + size)
                return cur - s;
        }
    }
    free(route_array);

    rc = snprintf(cur, s + size - cur, "}>");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    return cur - s;
}

int facelet_snprintf_json(char * s, size_t size, const facelet_t * facelet, int indent)
{
    char * cur = s;
    int rc;

    assert(facelet);

    /* Header + key attributes (netdevice + family) */
    rc = snprintf(cur, s + size - cur, "%*s%s", 4 * indent, "", "{\n");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    /* id */
    rc = snprintf(cur, s + size - cur, "%*s%s: %d,\n", 4 * (indent+1), "", "\"id\"",
            facelet->id);
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    /* Status */
    rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "", "\"status\"",
            facelet_status_str[facelet->status]);
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    /* Family */
    rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "", "\"family\"",
            (facelet->family == AF_INET) ? "AF_INET" :
            (facelet->family == AF_INET6) ? "AF_INET6" :
            (facelet->family == AF_UNSPEC) ? "AF_UNSPEC" :
            "unknown");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    /* Netdevice */
    if (facelet_has_netdevice(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "",
                "\"netdevice\"",
                facelet->netdevice.name[0] ? facelet->netdevice.name : "*");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

    } else {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "",
                "\"netdevice\"", "*");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Netdevice type */
    if (facelet_has_netdevice_type(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "",
                "\"netdevice_type\"",
                netdevice_type_str[facelet->netdevice_type]);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Local ip address */
    if (facelet_has_local_addr(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"", 4 * (indent+1), "",
                "\"local_addr\"");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = ip_address_snprintf(cur, s + size - cur, &facelet->local_addr,
                facelet->family);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = snprintf(cur, s + size - cur, "\",\n");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Local port */
    if (facelet_has_local_port(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: %d,\n", 4 * (indent+1), "",
                "\"local_port\"",
                facelet->local_port);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Remote ip address */
    if (facelet_has_remote_addr(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"", 4 * (indent+1), "",
                "\"remote_addr\"");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = ip_address_snprintf(cur, s + size - cur, &facelet->remote_addr,
                facelet->family);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;

        rc = snprintf(cur, s + size - cur, "\",\n");
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Remote port */
    if (facelet_has_remote_port(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: %d,\n", 4 * (indent+1), "",
                "\"remote_port\"",
                facelet->remote_port);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Admin state */
    if (facelet_has_admin_state(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "",
                "\"admin_state\"",
                face_state_str[facelet->admin_state]);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* State */
    if (facelet_has_state(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\",\n", 4 * (indent+1), "",
                "\"state\"",
                face_state_str[facelet->state]);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

#ifdef WITH_POLICY
    /* Priority */
    if (facelet_has_priority(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: %d,\n", 4 * (indent+1), "",
                "\"priority\"", facelet->priority);
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }
#endif /* WITH_POLICY */

    if (facelet_has_face_type(facelet)) {
        rc = snprintf(cur, s + size - cur, "%*s%s: \"LAYER%s/%s\",\n", 4 * (indent+1), "",
                "\"face_type\"",
            FACEMGR_FACE_TYPE_STR(facelet->face_type));
        if (rc < 0)
            return rc;
        cur += rc;
        if (cur >= s + size)
            return cur - s;
    }

    /* Status error */
    rc = snprintf(cur, s + size - cur, "%*s%s: \"%s\"\n", 4 * (indent+1), "",
            "\"error\"",
            facelet_get_error(facelet) ? "true" : "false");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    /* Routes */
    // TODO

    rc = snprintf(cur, s + size - cur, "%*s%s", 4 * indent, "", "}");
    if (rc < 0)
        return rc;
    cur += rc;
    if (cur >= s + size)
        return cur - s;

    return cur - s;
}