File: fcoemon.c

package info (click to toggle)
fcoe-utils 1.0.31+git20160622.5dfd3e4-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,060 kB
  • sloc: ansic: 11,919; sh: 1,298; makefile: 81
file content (3851 lines) | stat: -rw-r--r-- 92,969 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
/*
 * Copyright(c) 2010-2011 Intel Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Maintained at www.Open-FCoE.org
 */

#define __STDC_FORMAT_MACROS 1
#include <ctype.h>
#include <getopt.h>
#include <inttypes.h>
#include <malloc.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <libgen.h>
#include <ulimit.h>
#include <unistd.h>
#include <paths.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/queue.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <linux/if_arp.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>
#include <linux/dcbnl.h>

#include <lldpad/dcb_types.h>
#include <lldpad/clif.h>
#include <lldpad/lldp_dcbx_cmds.h>

#include "scsi_netlink_fc.h"
#include "fcoe_utils_version.h"
#include "fcoemon_utils.h"
#include "fcoemon.h"
#include "fcoe_clif.h"
#include "fcoe_utils.h"
#include "sysfs_hba.h"
#include "strarr.h"

#include "fip.h"
#include "rtnetlink.h"

#ifndef SYSCONFDIR
#define SYSCONFDIR                  "/etc"
#endif

#define CONFIG_DIR                  SYSCONFDIR "/fcoe"
#define CONFIG_MIN_VAL_LEN          (1 + 2)
#define CONFIG_MAX_VAL_LEN          (20 + 2)
#define DCB_APP_0_DEFAULT_ENABLE    1
#define DCB_APP_0_DEFAULT_WILLING   1
#define FILE_NAME_LEN               (NAME_MAX + 1)
#define CFG_FILE_PREFIX             "cfg-"
#define DEF_CFG_FILE                CFG_FILE_PREFIX "ethx"
#define FCOE_VLAN_SUFFIX            "-fcoe"
#define FCOE_VLAN_FORMAT            "%s.%d" FCOE_VLAN_SUFFIX
#define FCOE_VID_SCAN_FORMAT        "%*[^.].%d" FCOE_VLAN_SUFFIX

#define VLAN_DIR                "/proc/net/vlan"

#define DCBD_CONNECT_TIMEOUT    (10 * 1000 * 1000)	/* 10 seconds */
#define DCBD_CONNECT_RETRY_TIMEOUT   (1 * 1000 * 1000)	/* 1 seconds */
#define DCBD_REQ_RETRY_TIMEOUT  (200 * 1000)            /* 0.2 seconds */
#define DCBD_MAX_REQ_RETRIES    10
#define FCM_PING_REQ_LEN	1 /* byte-length of dcbd PING request */
#define FCM_PING_RSP_LEN	8 /* byte-length of dcbd PING response */

#define FCM_VLAN_DISC_TIMEOUT	(1000 * 1000)	/* 1 seconds */

#define DEF_RX_BUF_SIZE		4096

#define NLA_DATA(nla)        ((void *)((char *)(nla) + NLA_HDRLEN))
#define NLA_NEXT(nla) (struct rtattr *)((char *)nla + NLMSG_ALIGN(nla->rta_len))

#define FCOE_ETH_TYPE	0x8906

#define CFG_IF_VAR_FCOEENABLE  "FCOE_ENABLE"
#define CFG_IF_VAR_DCBREQUIRED "DCB_REQUIRED"
#define CFG_IF_VAR_AUTOVLAN    "AUTO_VLAN"
#define CFG_IF_VAR_MODE        "MODE"
#define CFG_IF_VAR_FIP_RESP    "FIP_RESP"

enum fcoe_mode {
	FCOE_MODE_FABRIC = 0,
	FCOE_MODE_VN2VN = 1,
};

static bool force_legacy;
static sigset_t block_sigset;

void fcm_vlan_disc_timeout(void *arg);

/*
 * fcoe service configuration data
 * Note: These information are read in from the fcoe service
 *       files in CONFIG_DIR
 */
struct fcoe_port {
	struct fcoe_port *next;

	/* information from fcoe configuration files in CONFIG_DIR */
	char ifname[IFNAMSIZ];       /* netif on which fcoe i/f is created */
	char real_ifname[IFNAMSIZ];  /* underlying net ifname - e.g. if ifname
					is a VLAN */
	int fcoe_enable;
	int dcb_required;
	enum fcoe_mode mode;
	bool fip_resp;
	int auto_vlan;
	int auto_created;
	int ready;

	/* following track data required to manage FCoE interface state */
	enum fcp_action action;      /* current state */
	enum fcp_action last_action; /* last action */
	int last_msg_type;     /* last rtnetlink msg type received on if name */
	struct sock_info *sock_reply;

	int ifindex;
	unsigned char mac[ETHER_ADDR_LEN];
	struct sa_timer vlan_disc_timer;
	int vlan_disc_count;
	int fip_socket;
	int fip_responder_socket;
	char fchost[FCHOSTBUFLEN];
	char ctlr[FCHOSTBUFLEN];
	uint32_t last_fc_event_num;
};

enum fcoeport_ifname {
	FCP_CFG_IFNAME = 0,
	FCP_REAL_IFNAME
};

/*
 * Interact with DCB daemon.
 */
static void fcm_dcbd_timeout(void *);
static void fcm_dcbd_retry_timeout(void *);
static void fcm_dcbd_disconnect(void);
static int fcm_dcbd_request(char *);
static void fcm_dcbd_rx(void *);
static void fcm_dcbd_event(char *, size_t);
static void fcm_dcbd_cmd_resp(char *, cmd_status);
static void fcm_netif_advance(struct fcm_netif *);
static void fcm_fcoe_action(struct fcoe_port *);
static void fcp_set_next_action(struct fcoe_port *, enum fcp_action);
static enum fcoe_status fcm_fcoe_if_action(char *, char *);

/*
 * Used for backwards compatibility amongst libfcoe
 * "control" interfaces.
 */
struct libfcoe_interface_template {
	enum fcoe_status (*create)(struct fcoe_port *);
	enum fcoe_status (*destroy)(struct fcoe_port *);
	enum fcoe_status (*enable)(struct fcoe_port *);
	enum fcoe_status (*disable)(struct fcoe_port *);
};

static const struct libfcoe_interface_template *libfcoe_control;

static enum fcoe_status fcm_module_create(struct fcoe_port *p)
{
	enum fcoe_status rc;

	switch (p->mode) {
	case FCOE_MODE_VN2VN:
		rc = fcm_fcoe_if_action(FCOE_CREATE_VN2VN, p->ifname);
		break;

	case FCOE_MODE_FABRIC:
	default:
		rc = fcm_fcoe_if_action(FCOE_CREATE, p->ifname);
		break;
	}
	if (rc)
		return rc;

	/*
	 * This call validates that the interface name
	 * has an active fcoe session by checking for
	 * the fc_host in sysfs.
	 */
	if (fcoe_find_fchost(p->ifname, p->fchost, FCHOSTBUFLEN)) {
		FCM_LOG_DBG("Failed to find fc_host for %s\n", p->ifname);
		return ENOSYSFS;
	}

	return SUCCESS;
}

static enum fcoe_status fcm_module_destroy(struct fcoe_port *p)
{
	return fcm_fcoe_if_action(FCOE_DESTROY, p->ifname);
}

static enum fcoe_status fcm_module_enable(struct fcoe_port *p)
{
	return fcm_fcoe_if_action(FCOE_ENABLE, p->ifname);
}

static enum fcoe_status fcm_module_disable(struct fcoe_port *p)
{
	return fcm_fcoe_if_action(FCOE_DISABLE, p->ifname);
}

static struct libfcoe_interface_template libfcoe_module_tmpl = {
	.create = fcm_module_create,
	.destroy = fcm_module_destroy,
	.enable = fcm_module_enable,
	.disable = fcm_module_disable,
};

static enum fcoe_status fcm_bus_enable(struct fcoe_port *p)
{
	return fcm_write_str_to_ctlr_attr(p->ctlr, FCOE_CTLR_ATTR_ENABLED, "1");
}

static int fcm_bus_configure(struct fcoe_port *p)
{
	int rc;

	if (p->mode != FCOE_MODE_VN2VN)
		return 0;

	rc = fcm_write_str_to_ctlr_attr(p->ctlr, FCOE_CTLR_ATTR_MODE, "vn2vn");
	return rc;
}

static enum fcoe_status fcm_bus_create(struct fcoe_port *p)
{
	enum fcoe_status rc;

	rc = fcm_write_str_to_sysfs_file(FCOE_BUS_CREATE, p->ifname);
	if (rc)
		return rc;

	/*
	 * This call validates that the interface name
	 * has an active fcoe session by checking for
	 * the fc_host in sysfs.
	 */
	if (fcoe_find_fchost(p->ifname, p->fchost, FCHOSTBUFLEN)) {
		FCM_LOG_DBG("Failed to find fc_host for %s\n", p->ifname);
		return ENOSYSFS;
	}

	/*
	 * The fcoe_ctlr_device lookup only happens when the fcoe_sysfs
	 * kernel interfaces are used. It is a defect if p->ctlr is used
	 * outside of these abstracted routines.
	 */
	if (fcoe_find_ctlr(p->fchost, p->ctlr, FCHOSTBUFLEN)) {
		FCM_LOG_DBG("Failed to get ctlr for %s\n", p->ifname);
		return ENOSYSFS;
	}

	rc = fcm_bus_configure(p);
	if (!rc)
		rc = fcm_bus_enable(p);

	return rc;
}

static enum fcoe_status fcm_bus_destroy(struct fcoe_port *p)
{
	return fcm_write_str_to_sysfs_file(FCOE_BUS_DESTROY, p->ifname);
}

static enum fcoe_status fcm_bus_disable(struct fcoe_port *p)
{
	return fcm_write_str_to_ctlr_attr(p->ctlr, FCOE_CTLR_ATTR_ENABLED, "0");
}

static struct libfcoe_interface_template libfcoe_bus_tmpl = {
	.create = fcm_bus_create,
	.destroy = fcm_bus_destroy,
	.enable = fcm_bus_enable,
	.disable = fcm_bus_disable,
};

struct fcm_clif {
	int cl_fd;
	int cl_busy;		/* non-zero if command pending */
	int cl_ping_pending;
	struct sockaddr_un cl_local;
};

static struct fcm_clif fcm_clif_st;
static struct fcm_clif *fcm_clif = &fcm_clif_st;
static struct sa_timer fcm_dcbd_timer;

/* Debugging routine */
static void print_errors(int errors);

struct fcm_netif_head fcm_netif_head = TAILQ_HEAD_INITIALIZER(fcm_netif_head);

static int fcm_fc_socket;

static int fcm_link_socket;
static int fcm_link_seq;
static void fcm_link_recv(void *);
static void fcm_link_getlink(void);
static int fcm_link_buf_check(size_t);
static void clear_dcbd_info(struct fcm_netif *ff);
static int fcoe_vid_from_ifname(const char *ifname);

/*
 * Table for getopt_long(3).
 */
static struct option fcm_options[] = {
	{"debug", 0, NULL, 'd'},
	{"legacy", 0, NULL, 'l'},
	{"syslog", 0, NULL, 's'},
	{"exec", 1, NULL, 'e'},
	{"foreground", 0, NULL, 'f'},
	{"version", 0, NULL, 'v'},
	{NULL, 0, NULL, 0}
};

char progname[20];

/*
 * Issue with buffer size:  It isn't clear how to read more than one
 * buffer's worth of GETLINK replies.  The kernel seems to just drop the
 * interface messages if they don't fit in the buffer, so we just make it
 * large enough to fit and expand it if we ever do a read that almost fills it.
 */
static char *fcm_link_buf;
static size_t fcm_link_buf_size = 4096;	/* initial size */
static const size_t fcm_link_buf_fuzz = 300;	/* "almost full" remainder */

/*
 * A value must be surrounded by quates, e.g. "x".
 * The minimum length of a value is 1 excluding the quotes.
 * The maximum length of a value is 20 excluding the quotes.
 */
static int fcm_remove_quotes(char *buf, int len)
{
	char *s = buf;
	char *e = buf + len - 1;
	char tmp[CONFIG_MAX_VAL_LEN + 1];

	if (len < CONFIG_MIN_VAL_LEN)
		return -1;
	if ((*s >= '0' && *s <= '9') ||
	    (*s >= 'a' && *s <= 'z') ||
	    (*s >= 'A' && *s <= 'Z'))
		return -1;
	if ((*e >= '0' && *e <= '9') ||
	    (*e >= 'a' && *e <= 'z') ||
	    (*e >= 'A' && *e <= 'Z'))
		return -1;
	s = buf + 1;
	*e = '\0';
	strncpy(tmp, s, len - 1);
	strncpy(buf, tmp, len - 1);

	return 0;
}

/*
 * Read a configuration variable for a port from a config file.
 * There's no problem if the file doesn't exist.
 * The buffer is set to an empty string if the variable is not found.
 *
 * Returns:  1    found
 *           0    not found
 *           -1   error in format
 */
static size_t fcm_read_config_variable(char *file, char *val_buf, size_t len,
				       FILE *fp, const char *var_name)
{
	char *s;
	char *var;
	char *val;
	char buf[FILE_NAME_LEN];
	int n;

	val_buf[0] = '\0';
	buf[sizeof(buf) - 1] = '\0';
	rewind(fp);
	while ((s = fgets(buf, sizeof(buf) - 1, fp)) != NULL) {
		while (isspace(*s))
			s++;
		if (*s == '\0' || *s == '#')
			continue;
		var = s;
		if (!isalpha(*var))
			continue;
		val = strchr(s, '=');
		if (val == NULL)
			continue;
		*val++ = '\0';
		s = val;
		if (strcmp(var_name, var) != 0)
			continue;
		while (*s != '\0' && !isspace(*s))
			s++;
		*s = '\0';
		n = snprintf(val_buf, len, "%s", val);
		if (fcm_remove_quotes(val_buf, n) < 0) {
			FCM_LOG("Invalid format in config file"
				" %s: %s=%s\n",
				file, var_name, val);
			/* error */
			return -1;
		}
		/* found */
		FCM_LOG_DBG("%s: %s = %s\n", file, var_name, val);
		return 1;
	}
	/* not found */
	return 0;
}

static struct fcoe_port *alloc_fcoe_port(char *ifname)
{
	struct fcoe_port *p = NULL;

	p = (struct fcoe_port *) calloc(1, sizeof(struct fcoe_port));
	if (p) {
		snprintf(p->ifname, sizeof(p->ifname), "%s", ifname);
		p->action = FCP_WAIT;
		/* last_action is initialized to FCP_DESTROY_IF to indicate
		 * that the interface is not created yet.
		 */
		p->last_action = FCP_DESTROY_IF;
		p->fip_socket = -1;
		p->fip_responder_socket = -1;
		p->fchost[0] = '\0';
		p->last_fc_event_num = 0;
		sa_timer_init(&p->vlan_disc_timer, fcm_vlan_disc_timeout, p);
		p->ready = 1;
	}

	return p;
}

static bool real_ifname_from_name(char *real_ifname, const char *ifname)
{
	const char *sep;

	sep = index(ifname, '.');
	if (!sep)
		return false;
	memset(real_ifname, 0, IFNAMSIZ);
	memcpy(real_ifname, ifname, sep - ifname);
	return true;
}

static int fcm_read_config_files(void)
{
	char file[80];
	FILE *fp;
	char val[CONFIG_MAX_VAL_LEN + 1];
	DIR *dir;
	struct dirent *dp;
	struct fcoe_port *curr = NULL;
	struct fcoe_port *next = NULL;
	int rc;

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	dir = opendir(CONFIG_DIR);
	if (dir == NULL) {
		FCM_LOG_ERR(errno, "Failed reading directory %s\n", CONFIG_DIR);
		return -1;
	}
	for (;;) {
		dp = readdir(dir);
		if (dp == NULL)
			break;
		if (dp->d_name[0] == '.' &&
		    (dp->d_name[1] == '\0' ||
		     (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
			continue;
		rc = strncmp(dp->d_name, CFG_FILE_PREFIX,
			     strlen(CFG_FILE_PREFIX));
		if (rc)
			continue;

		if (!strncmp(dp->d_name, DEF_CFG_FILE,
			     strlen(DEF_CFG_FILE)))
			continue;

		next = alloc_fcoe_port(dp->d_name + 4);

		if (!next) {
			FCM_LOG_ERR(errno, "failed to allocate fcoe_port %s",
				    dp->d_name);
			continue;
		}
		strncpy(file, CONFIG_DIR "/", sizeof(file));
		strncat(file, dp->d_name, sizeof(file) - strlen(file));
		fp = fopen(file, "r");
		if (!fp) {
			FCM_LOG_ERR(errno, "Failed to read %s\n", file);
			free(next);
			continue;
		}

		real_ifname_from_name(next->real_ifname, next->ifname);

		/* FCOE_ENABLE */
		rc = fcm_read_config_variable(file, val, sizeof(val),
					      fp, CFG_IF_VAR_FCOEENABLE);
		if (rc < 0) {
			FCM_LOG("Invalid format for %s variable in %s",
				CFG_IF_VAR_FCOEENABLE, file);
			fclose(fp);
			free(next);
			continue;
		}
		/* if not found, default to "no" */
		if (!strncasecmp(val, "yes", 3) && rc == 1)
			next->fcoe_enable = 1;

		/* DCB_REQUIRED */
		rc = fcm_read_config_variable(file, val, sizeof(val),
					      fp, CFG_IF_VAR_DCBREQUIRED);
		if (rc < 0) {
			FCM_LOG("Invalid format for %s variable in %s",
				CFG_IF_VAR_DCBREQUIRED, file);
			fclose(fp);
			free(next);
			continue;
		}
		/* if not found, default to "no" */
		if (!strncasecmp(val, "yes", 3) && rc == 1)
			next->dcb_required = 1;

		if (next->dcb_required == 1 && fcoe_config.dcb_init == 0)
			fcoe_config.dcb_init = 1;

		/* AUTO_VLAN */
		rc = fcm_read_config_variable(file, val, sizeof(val),
					      fp, CFG_IF_VAR_AUTOVLAN);
		if (rc < 0) {
			FCM_LOG("Invalid format for %s variable in %s",
				CFG_IF_VAR_AUTOVLAN, file);
			fclose(fp);
			free(next);
			continue;
		}
		/* if not found, default to "no" */
		if (!strncasecmp(val, "yes", 3) && rc == 1)
			next->auto_vlan = 1;

		/* FIP_RESP */
		rc = fcm_read_config_variable(file, val, sizeof(val),
					      fp, CFG_IF_VAR_FIP_RESP);
		if (rc < 0) {
			FCM_LOG("Invalid format for %s variable in %s",
				CFG_IF_VAR_FIP_RESP, file);
			fclose(fp);
			free(next);
			continue;
		}
		/* if not found, default to "none" */
		next->fip_resp = false;
		if (!strcasecmp(val, "yes") && rc == 1) {
			FCM_LOG("Starting FIP responder on %s", next->ifname);
			next->fip_resp = true;
		}

		/* MODE */
		rc = fcm_read_config_variable(file, val, sizeof(val),
					      fp, CFG_IF_VAR_MODE);
		if (rc < 0) {
			FCM_LOG("Invalid format for %s variable in %s",
				CFG_IF_VAR_MODE, file);
			fclose(fp);
			free(next);
			continue;
		}
		/* if not found, default to "fabric" */
		next->mode = FCOE_MODE_FABRIC;
		if (!strncasecmp(val, "vn2vn", 5) && rc == 1)
			next->mode = FCOE_MODE_VN2VN;

		fclose(fp);

		if (!fcoe_config.port) {
			fcoe_config.port = next;
			curr = next;
		} else {
			curr->next = next;
			curr = next;
		}
	}
	closedir(dir);

	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);

	return 0;
}

/*
 * Given an fcoe_port pointer and an ifname, find the next fcoe_port
 * in the list with a real ifname of 'ifname'.
 *
 * Returns:  fcoe_port pointer to fcoe port entry
 *           NULL - if not found
 */
static struct fcoe_port *fcm_find_next_fcoe_port(struct fcoe_port *p,
						 char *ifname)
{
	struct fcoe_port *np;
	struct fcoe_port *found_port = NULL;

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	np = fcoe_config.port;
	while (np) {
		if (np == p)
			break;
		np = np->next;
	}

	if (np)
		np = np->next;

	while (np) {
		if (!strncmp(ifname, np->real_ifname, IFNAMSIZ)) {
			found_port = np;
			break;
		}
		np = np->next;
	}

	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);

	return found_port;
}

static struct fcoe_port *fcm_find_fcoe_port(char *ifname,
					    enum fcoeport_ifname t)
{
	struct fcoe_port *p;
	struct fcoe_port *found_port = NULL;
	char *fp_ifname;

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	p = fcoe_config.port;
	while (p) {
		switch (t) {
		case FCP_CFG_IFNAME:
			fp_ifname = p->ifname;
			break;
		case FCP_REAL_IFNAME:
			fp_ifname = p->real_ifname;
			break;
		default:
			FCM_LOG("unhandled interface type [%d] for %s",
				t, ifname);
			goto found;
		}

		if (!strncmp(ifname, fp_ifname, IFNAMSIZ)) {
			found_port = p;
			goto found;
		}

		p = p->next;
	}

found:
	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);
	return found_port;
}

static struct fcoe_port *fcm_find_port_by_host(uint16_t host_no)
{
	struct fcoe_port *p;
	struct fcoe_port *found_port = NULL;
	char host[FCHOSTBUFLEN];

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	snprintf(host, FCHOSTBUFLEN, "host%d", host_no);
	p = fcoe_config.port;
	while (p) {
		if (!strncmp(p->fchost, host, FCHOSTBUFLEN)) {
			found_port = p;
			break;
		}
		p = p->next;
	}

	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);

	return found_port;
}

static void fcm_fc_event_handler(struct fc_nl_event *fc_event)
{
	struct fcoe_port *p = fcm_find_port_by_host(fc_event->host_no);

	if (!p)
		return;

	switch (fc_event->event_code) {
	case HBA_EVENT_LIP_RESET_OCCURRED:
		if (!p->last_fc_event_num &&
		    fc_event->event_num == p->last_fc_event_num)
			return;

		if (!p->auto_created && !p->auto_vlan)
			return;

		p->last_fc_event_num = fc_event->event_num;

		/* find real interface port and re-activate again */
		p = fcm_find_fcoe_port(p->real_ifname, FCP_CFG_IFNAME);
		if (p && p->last_action != FCP_DISABLE_IF)
			fcp_set_next_action(p, FCP_ACTIVATE_IF);
		break;
	default:
		FCM_LOG("unsupported fc event:%d for host:%d\n",
			fc_event->event_code, fc_event->host_no);
	}
}

static void log_nlmsg_error(struct nlmsghdr *hp, size_t rlen, const char *str)
{
	struct nlmsgerr *ep;

	if (NLMSG_OK(hp, rlen)) {
		ep = (struct nlmsgerr *)NLMSG_NEXT(hp, rlen);
		FCM_LOG_DBG("%s, err=%d, type=%d\n",
			    str, ep->error, ep->msg.nlmsg_type);
	} else {
		FCM_LOG("%s", str);
	}
}

static void fcm_fc_event_log(struct fc_nl_event *fe)
{
	/* from kernel "include/scsi/scsi_transport_fc.h" */
	enum fc_host_event_code  {
		FCH_EVT_LIP                     = 0x1,
		FCH_EVT_LINKUP                  = 0x2,
		FCH_EVT_LINKDOWN                = 0x3,
		FCH_EVT_LIPRESET                = 0x4,
		FCH_EVT_RSCN                    = 0x5,
		FCH_EVT_ADAPTER_CHANGE          = 0x103,
		FCH_EVT_PORT_UNKNOWN            = 0x200,
		FCH_EVT_PORT_OFFLINE            = 0x201,
		FCH_EVT_PORT_ONLINE             = 0x202,
		FCH_EVT_PORT_FABRIC             = 0x204,
		FCH_EVT_LINK_UNKNOWN            = 0x500,
		FCH_EVT_VENDOR_UNIQUE           = 0xffff,
	};
	/* from kernel "drivers/scsi/scsi_transport_fc.c" */
	const struct {
		enum fc_host_event_code         value;
		char                            *name;
	} fc_host_event_code_names[] = {
		{ FCH_EVT_LIP,                  "lip" },
		{ FCH_EVT_LINKUP,               "link_up" },
		{ FCH_EVT_LINKDOWN,             "link_down" },
		{ FCH_EVT_LIPRESET,             "lip_reset" },
		{ FCH_EVT_RSCN,                 "rscn" },
		{ FCH_EVT_ADAPTER_CHANGE,       "adapter_chg" },
		{ FCH_EVT_PORT_UNKNOWN,         "port_unknown" },
		{ FCH_EVT_PORT_ONLINE,          "port_online" },
		{ FCH_EVT_PORT_OFFLINE,         "port_offline" },
		{ FCH_EVT_PORT_FABRIC,          "port_fabric" },
		{ FCH_EVT_LINK_UNKNOWN,         "link_unknown" },
		{ FCH_EVT_VENDOR_UNIQUE,        "vendor_unique" },
	};
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(fc_host_event_code_names); i++) {
		if (fe->event_code == fc_host_event_code_names[i].value) {
			/* only do u32 data even len is not, e.g. vendor */
			FCM_LOG_DBG("FC_HOST_EVENT %d at %" PRIu64 " secs on "
				    "host%d code %d=%s datalen %d data=%d\n",
				    fe->event_num, fe->seconds,
				    fe->host_no, fe->event_code,
				    fc_host_event_code_names[i].name,
				    fe->event_datalen, fe->event_data);
			break;
		}
	}

}

static void fcm_fc_event_recv(UNUSED void *arg)
{
	struct nlmsghdr *hp;
	struct fc_nl_event *fc_event;
	size_t plen;
	size_t rlen;
	char *buf;
	int rc;

	buf = malloc(DEF_RX_BUF_SIZE);

	if (!buf) {
		FCM_LOG_ERR(errno, "failed to allocate FC event buffer\n");
		return;
	}

	rc = read(fcm_fc_socket, buf, DEF_RX_BUF_SIZE);
	if (!rc)
		goto free_buf;

	if (rc < 0) {
		FCM_LOG_ERR(errno, "fc read error");
		goto free_buf;
	}

	hp = (struct nlmsghdr *)buf;
	rlen = rc;
	for (hp = (struct nlmsghdr *)buf; NLMSG_OK(hp, rlen);
	     hp = NLMSG_NEXT(hp, rlen)) {

		if (hp->nlmsg_type == NLMSG_DONE)
			break;

		if (hp->nlmsg_type == NLMSG_ERROR) {
			log_nlmsg_error(hp, rlen, "fc nlmsg error");
			break;
		}

		plen = NLMSG_PAYLOAD(hp, 0);
		fc_event = (struct fc_nl_event *)NLMSG_DATA(hp);
		if (plen < sizeof(*fc_event)) {
			FCM_LOG("too short (%zu) to be an FC event", rlen);
			break;
		}
		fcm_fc_event_log(fc_event);
		fcm_fc_event_handler(fc_event);
	}
free_buf:
	free(buf);
}

static int fcm_fc_events_init(void)
{
	int fd, rc;
	struct sockaddr_nl fc_local;

	fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_SCSITRANSPORT);
	if (fd < 0) {
		FCM_LOG_ERR(errno, "fc socket error");
		return fd;
	}
	memset(&fc_local, 0, sizeof(fc_local));
	fc_local.nl_family = AF_NETLINK;
	fc_local.nl_groups = ~0;
	fc_local.nl_pid = getpid();
	rc = bind(fd, (struct sockaddr *)&fc_local, sizeof(fc_local));
	if (rc == -1) {
		FCM_LOG_ERR(errno, "fc socket bind error");
		close(fd);
		return rc;
	}
	fcm_fc_socket = fd;

	/* Add a given file descriptor readfds set with its rx handler */
	sa_select_add_fd(fd, fcm_fc_event_recv, NULL, NULL, NULL);
	return 0;
}

static int fcm_link_init(void)
{
	int fd;
	int rc;
	struct sockaddr_nl l_local;

	fcm_link_buf = malloc(fcm_link_buf_size);
	ASSERT(fcm_link_buf);

	fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
	if (fd < 0) {
		FCM_LOG_ERR(errno, "socket error");
		return fd;
	}
	memset(&l_local, 0, sizeof(l_local));
	l_local.nl_family = AF_NETLINK;
	l_local.nl_groups = RTMGRP_LINK | (1 << (RTNLGRP_DCB - 1));
	l_local.nl_pid = 0;
	rc = bind(fd, (struct sockaddr *)&l_local, sizeof(l_local));
	if (rc == -1) {
		FCM_LOG_ERR(errno, "bind error");
		close(fd);
		return rc;
	}
	fcm_link_socket = fd;

	/* Add a given file descriptor from a readfds set */
	sa_select_add_fd(fd, fcm_link_recv, NULL, NULL, NULL);

	fcm_link_getlink();

	return 0;
}

static struct fcoe_port *
fcm_port_create(char *ifname, enum clif_flags flags, int cmd);

static struct fcoe_port *fcm_new_vlan(int ifindex, int vid, bool vn2vn)
{
	char real_name[IFNAMSIZ];
	char vlan_name[IFNAMSIZ*2];
	struct fcoe_port *p;
	static const int flags[] = {
		[false] = CLIF_FLAGS_FABRIC,
		[true] = CLIF_FLAGS_VN2VN,
	};

	if (vn2vn)
		FCM_LOG_DBG("Auto VLAN found vn2vn on VID %d\n", vid);
	else
		FCM_LOG_DBG("Auto VLAN Found FCF on VID %d\n", vid);

	if (rtnl_find_vlan(ifindex, vid, vlan_name)) {
		rtnl_get_linkname(ifindex, real_name);
		snprintf(vlan_name, sizeof(vlan_name), FCOE_VLAN_FORMAT,
			 real_name, vid);
		vlan_create(ifindex, vid, vlan_name);
	}
	rtnl_set_iff_up(0, vlan_name);
	p = fcm_find_fcoe_port(vlan_name, FCP_CFG_IFNAME);
	if (p && !p->fcoe_enable)
		return p;
	p = fcm_port_create(vlan_name, flags[vn2vn], FCP_ACTIVATE_IF);
	p->auto_created = 1;
	return p;
}

static int
fcm_vlan_disc_handler(struct fiphdr *fh, struct sockaddr_ll *sa, void *arg)
{
	int vid;
	unsigned char mac[ETHER_ADDR_LEN];
	int len = ntohs(fh->fip_desc_len);
	struct fip_tlv_hdr *tlv = (struct fip_tlv_hdr *)(fh + 1);
	struct fcoe_port *p = arg;
	struct fcoe_port *vp;
	int desc_mask = 0;
	bool vn2vn = false;

	enum {
		VALID_MAC	= 1,
		VALID_VLAN	= 2,
	};

	if (ntohs(fh->fip_proto) != FIP_PROTO_VLAN)
		return -1;

	if (fh->fip_subcode == FIP_VLAN_NOTE_VN2VN &&
	    (!p->auto_vlan || p->mode != FCOE_MODE_VN2VN)) {
		FCM_LOG_DBG("%s: vn2vn vlan notif: auto_vlan=%d, mode=%d\n",
			    __func__, p->auto_vlan, p->mode);
		return -1;
	}

	if (fh->fip_subcode != FIP_VLAN_NOTE &&
	    fh->fip_subcode != FIP_VLAN_NOTE_VN2VN) {
		FCM_LOG_DBG("%s: fip_subcode=%d\n", __func__, fh->fip_subcode);
		return -1;
	}

	if (fh->fip_subcode == FIP_VLAN_NOTE_VN2VN)
		vn2vn = true;

	while (len > 0) {
		switch (tlv->tlv_type) {
		case FIP_TLV_MAC_ADDR:
			memcpy(mac, ((struct fip_tlv_mac_addr *)tlv)->mac_addr,
			       ETHER_ADDR_LEN);
			desc_mask |= VALID_MAC;
			break;
			/*
			 * this expects to see the MAC_ADDR TLV first,
			 * and is broken if not
			 */
		case FIP_TLV_VLAN:
			if (tlv->tlv_len != 1) {
				FCM_LOG_ERR(EINVAL, "bad length on VLAN TLV");
				break;
			}
			vid = ntohs(((struct fip_tlv_vlan *)tlv)->vlan);
			FCM_LOG_DBG("%s: vid=%d\n", __func__, vid);
			if (vid) {
				vp = fcm_new_vlan(sa->sll_ifindex, vid, vn2vn);
				vp->dcb_required = p->dcb_required;
			} else {
				/* We received a 0 vlan id. Activate the
				 * physical port itself
				 */
				fcp_set_next_action(p, FCP_ACTIVATE_IF);
				p->auto_vlan = 0;
			}
			desc_mask |= VALID_VLAN;
			break;
		default:
			/* unexpected or unrecognized descriptor */
			FCM_LOG_DBG("ignoring TLV type %d", tlv->tlv_type);
			break;
		}
		len -= tlv->tlv_len;
		tlv = ((void *) tlv) + (tlv->tlv_len << 2);
	};

	if (desc_mask == (VALID_MAC | VALID_VLAN)) {
		/* cancel the retry timer, valid response received */
		sa_timer_cancel(&p->vlan_disc_timer);
		return 0;
	} else {
		return -1;
	}
}

static void fcm_fip_recv(void *arg)
{
	struct fcoe_port *p = arg;
	fip_recv(p->fip_socket, fcm_vlan_disc_handler, p);
}

static int fcm_vlan_disc_socket(struct fcoe_port *p)
{
	int fd;
	int origdev = 1;

	fd = fip_socket(p->ifindex, FIP_NONE);
	if (fd < 0) {
		FCM_LOG_ERR(errno, "socket error");
		return fd;
	}
	setsockopt(fd, SOL_PACKET, PACKET_ORIGDEV, &origdev, sizeof(origdev));
	sa_select_add_fd(fd, fcm_fip_recv, NULL, NULL, p);
	return fd;
}


/* fcm_vlan_dev_real_dev - query vlan real_dev
 * @vlan_ifname - vlan device ifname to find real interface name for
 * @real_ifname - pointer to copy real ifname to
 *
 * Make an ioctl call to find the real device for vlan_ifname.
 * Copy to real_ifname if found.
 */
static void fcm_vlan_dev_real_dev(char *vlan_ifname, char *real_ifname)
{
	int fd;
	struct vlan_ioctl_args ifv;

	real_ifname[0] = '\0';

	fd = socket(PF_INET, SOCK_DGRAM, 0);

	if (fd < 0) {
		FCM_LOG_ERR(errno, "open vlan query socket error");
		return;
	}

	memset(&ifv, 0, sizeof(ifv));
	ifv.cmd = GET_VLAN_REALDEV_NAME_CMD;
	strncpy(ifv.device1, vlan_ifname, strlen(vlan_ifname)+1);
	if (ioctl(fd, SIOCGIFVLAN, &ifv) == 0)
		strncpy(real_ifname, ifv.u.device2, strlen(ifv.u.device2)+1);
	close(fd);
}

/* fcm_is_linkinfo_vlan - parse nlmsg linkinfo rtattr for vlan kind
 * @ap: pointer to the linkinfo rtattr
 *
 * This function parses the linkinfo rtattr and returns
 * 1 if it is kind vlan otherwise returns 0.
 */
static int fcm_is_linkinfo_vlan(struct rtattr *ap)
{
	struct rtattr *info;
	int len;

	info = (struct rtattr *) (RTA_DATA(ap));

	for (len = ap->rta_len; RTA_OK(info, len); info = RTA_NEXT(info, len)) {
		if (info->rta_type != IFLA_INFO_KIND)
			continue;

		if (strncmp("vlan", RTA_DATA(info), sizeof("vlan")))
			return 0;
		else
			return 1;
	}

	return 0;
}


/* fcm_set_next_action - determine the next action for the FCoE interface
 * @p - pointer to the fcoe_port structure for the FCoE interface
 * @action - requested next action to take on the FCoE interface
 *
 * Based on the last_action taken on the FCoE interface and the requested
 * next action, the next action field in the FCoE interface's fcoe_port
 * structure is set.
 * Notes: last_action is initialized to FCP_DESTROY_IF when the fcoe_port is
 *        created and it is never set to FCP_WAIT.
 *        The requested action FCP_ACTIVATE_IF is resolved to either
 *        FCP_CREATE_IF or FCP_ENABLE_IF as appropriate.
 */
static void fcp_set_next_action(struct fcoe_port *p, enum fcp_action action)
{
	switch (p->last_action) {
	case FCP_CREATE_IF:
		switch (action) {
		case FCP_DESTROY_IF:
		case FCP_ENABLE_IF:
		case FCP_DISABLE_IF:
		case FCP_RESET_IF:
		case FCP_SCAN_IF:
			p->action = action;
			break;
		case FCP_ACTIVATE_IF:
			if (p->auto_vlan)
				p->action = FCP_VLAN_DISC;
			else
				p->action = FCP_ENABLE_IF;
			break;
		default:
			p->action = FCP_WAIT;
			break;
		}
		break;
	case FCP_DESTROY_IF:
		switch (action) {
		case FCP_CREATE_IF:
		case FCP_ACTIVATE_IF:
		case FCP_ENABLE_IF:
			if (p->auto_vlan)
				p->action = FCP_VLAN_DISC;
			else if (p->fcoe_enable)
				p->action = FCP_CREATE_IF;
			else
				p->action = FCP_WAIT;
			break;
		default:
			p->action = FCP_WAIT;
			break;
		}
		break;
	case FCP_ENABLE_IF:
		switch (action) {
		case FCP_DESTROY_IF:
		case FCP_DISABLE_IF:
		case FCP_RESET_IF:
		case FCP_SCAN_IF:
			p->action = action;
			break;
		default:
			p->action = FCP_WAIT;
			break;
		}
		break;
	case FCP_DISABLE_IF:
		switch (action) {
		case FCP_DESTROY_IF:
		case FCP_RESET_IF:
			p->action = action;
			break;
		case FCP_ENABLE_IF:
		case FCP_ACTIVATE_IF:
			if (p->auto_vlan)
				p->action = FCP_VLAN_DISC;
			else
				p->action = FCP_ENABLE_IF;
			break;
		default:
			p->action = FCP_WAIT;
			break;
		}
		break;
	case FCP_RESET_IF:
	case FCP_SCAN_IF:
		switch (action) {
		case FCP_DESTROY_IF:
		case FCP_DISABLE_IF:
		case FCP_RESET_IF:
		case FCP_SCAN_IF:
			p->action = action;
			break;
		case FCP_ENABLE_IF:
		case FCP_ACTIVATE_IF:
			if (p->auto_vlan)
				p->action = FCP_VLAN_DISC;
			else
				p->action = FCP_ENABLE_IF;
			break;
		default:
			p->action = FCP_WAIT;
			break;
		}
		break;
	case FCP_VLAN_DISC:
		switch (action) {
		case FCP_ACTIVATE_IF:
			if (p->auto_vlan)
				p->action = FCP_VLAN_DISC;
			else if (p->fcoe_enable)
				p->action = FCP_CREATE_IF;
			else
				p->action = FCP_WAIT;
			break;
		case FCP_DESTROY_IF:
		case FCP_DISABLE_IF:
		case FCP_RESET_IF:
		case FCP_SCAN_IF:
			if (p->fip_socket >= 0) {
				sa_timer_cancel(&p->vlan_disc_timer);
				sa_select_rem_fd(p->fip_socket);
				close(p->fip_socket);
				p->fip_socket = -1;
			}
			p->action = action;
			break;
		default:
			p->action = FCP_WAIT;
			break;
		}
		break;
	default:
		/* last_action is never set to FCP_WAIT */
		break;
	}
}

static void fcp_action_set(char *ifname, enum fcp_action action)
{
	struct fcoe_port *p;

	p = fcm_find_fcoe_port(ifname, FCP_REAL_IFNAME);
	while (p) {
		if (p->fcoe_enable) {
			switch (action) {
			case FCP_ACTIVATE_IF:
				/*
				 * let the VLAN discovery code
				 * enabled auto-VLANs
				 */
				if (!p->auto_created)
					fcp_set_next_action(p, FCP_ACTIVATE_IF);
				else
					fcp_set_next_action(p, FCP_WAIT);
				break;
			default:
				fcp_set_next_action(p, action);
			}
		}
		p = fcm_find_next_fcoe_port(p, ifname);
	}
}

/*
 * Send DCB_CMD_IEEE_GET request for an interface.
 */
static void ieee_get_req(struct fcm_netif *ff)
{
	int iflen;
	int rc;
	int seq;
	struct {
		struct nlmsghdr nl;
		struct dcbmsg dcbmsg;
		struct rtattr rta;
		char ifname[IFNAMSIZ];
	} msg;

	seq = ++fcm_link_seq;
	if (!seq)
		seq = ++fcm_link_seq;

	iflen = strlen(ff->ifname);
	if (iflen >= IFNAMSIZ)
		iflen = IFNAMSIZ - 1;

	memset(&msg, 0, sizeof(msg));
	msg.nl.nlmsg_len = NLMSG_ALIGN(sizeof(msg) - sizeof(msg.ifname) +
				iflen + 1);
	msg.nl.nlmsg_type = RTM_GETDCB;
	msg.nl.nlmsg_flags = NLM_F_REQUEST;
	msg.nl.nlmsg_seq = seq;
	msg.nl.nlmsg_pid = getpid();
	msg.dcbmsg.cmd = DCB_CMD_IEEE_GET;
	msg.dcbmsg.dcb_family = AF_UNSPEC;
	msg.dcbmsg.dcb_pad = 0;
	msg.rta.rta_len = NLMSG_ALIGN(NLA_HDRLEN + iflen + 1);
	msg.rta.rta_type = DCB_ATTR_IFNAME;
	strncpy(msg.ifname, ff->ifname, iflen);
	ff->ieee_resp_pending = seq;
	rc = write(fcm_link_socket, &msg, msg.nl.nlmsg_len);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "%s: %s: write failed\n", __func__,
			    ff->ifname);
		ff->ieee_resp_pending = 0;
	}
}

/*
 * clear_ieee_info - Clear IEEE info to unknown values
 */
static void clear_ieee_info(struct fcm_netif *ff)
{
	ff->ieee_pfc_info = 0;
	ff->ieee_app_info = 0;
	ff->dcbx_cap = 0;
}

STR_ARR(ieee_states, "Unknown", "Out of range",
	[IEEE_INIT] = "IEEE_INIT",
	[IEEE_GET_STATE] = "IEEE_GET_STATE",
	[IEEE_DONE] = "IEEE_DONE",
	[IEEE_ACTIVE] = "IEEE_ACTIVE",
);

static void
ieee_state_set(struct fcm_netif *ff, enum ieee_state new_state)
{
	if (ff->ff_operstate != IF_OPER_UP) {
		ff->ieee_state = IEEE_INIT;
		return;
	}

	if (fcoe_config.debug) {
		FCM_LOG_DEV_DBG(ff, "IEEE state change: %s -> %s",
				getstr(&ieee_states, ff->ieee_state),
				getstr(&ieee_states, new_state));
	}

	if (new_state == IEEE_GET_STATE)
		clear_ieee_info(ff);

	ff->ieee_state = new_state;
	ff->ieee_resp_pending = 0;
}

static struct sa_nameval fcm_dcbd_states[] = FCM_DCBD_STATES;

static void fcm_dcbd_state_set(struct fcm_netif *ff,
			       enum fcm_dcbd_state new_state)
{
	if (ff->ff_operstate != IF_OPER_UP) {
		ff->ff_dcbd_state = FCD_INIT;
		return;
	}

	if (fcoe_config.debug) {
		char old[32];
		char new[32];

		FCM_LOG_DEV_DBG(ff, "DCBD state change: %s -> %s",
				sa_enum_decode(old, sizeof(old),
					       fcm_dcbd_states,
					       ff->ff_dcbd_state),
				sa_enum_decode(new, sizeof(new),
					       fcm_dcbd_states, new_state));
	}

	if (new_state == FCD_GET_DCB_STATE)
		clear_dcbd_info(ff);

	if (new_state == FCD_INIT) {
		ff->dcbd_retry_cnt = 0;
		sa_timer_cancel(&ff->dcbd_retry_timer);
	}

	if (new_state == FCD_ERROR) {
		ff->dcbd_retry_cnt++;
		FCM_LOG_DEV_DBG(ff, "%s: SETTING lldpad RETRY TIMER  = %d\n",
				ff->ifname,
				ff->dcbd_retry_cnt * DCBD_REQ_RETRY_TIMEOUT);
		sa_timer_set(&ff->dcbd_retry_timer,
			     ff->dcbd_retry_cnt * DCBD_REQ_RETRY_TIMEOUT);
	}

	ff->ff_dcbd_state = new_state;
	ff->response_pending = 0;
}

static int fip_recv_vlan_req(struct sockaddr_ll *ssa, struct fcoe_port *sp)
{
	struct fip_tlv_vlan *vlan_tlvs = NULL;
	int vlan_count = 0;
	struct fcoe_port *p;
	int rc;

	/* Handle all FCoE ports which are on VLANs over this ifname. */
	p = fcm_find_fcoe_port(sp->real_ifname, FCP_REAL_IFNAME);
	for (; p; p = fcm_find_next_fcoe_port(p, sp->real_ifname)) {
		int vid;
		struct fip_tlv_vlan *vtp;

		if (!p->ready)
			continue;
		if (p->mode != FCOE_MODE_VN2VN)
			continue;
		vid = fcoe_vid_from_ifname(p->ifname);
		if (vid < 0)
			continue;
		vtp = realloc(vlan_tlvs, sizeof(*vlan_tlvs));
		if (!vtp)
			break;
		memset(&vtp[vlan_count], 0, sizeof(*vtp));
		vtp[vlan_count].hdr.tlv_type = FIP_TLV_VLAN;
		vtp[vlan_count].hdr.tlv_len = 1;
		vtp[vlan_count].vlan = htons(vid);
		++vlan_count;
		vlan_tlvs = vtp;
	}

	FCM_LOG_DBG("%s: %d vlans found\n", __func__, vlan_count);
	rc = fip_send_vlan_notification(sp->fip_responder_socket,
					ssa->sll_ifindex, sp->mac,
					ssa->sll_addr, vlan_tlvs, vlan_count);
	if (rc < 0) {
		FCM_LOG_ERR(-rc, "%s: fip_send_vlan_notification error\n",
			    __func__);
	}
	if (vlan_tlvs)
		free(vlan_tlvs);
	return rc;
}

static int fip_vlan_disc_handler(struct fiphdr *fh, struct sockaddr_ll *sa,
				 void *arg)
{
	struct fcoe_port *p = arg;
	int rc = -1;

	if (ntohs(fh->fip_proto) != FIP_PROTO_VLAN) {
		FCM_LOG_DBG("ignoring FIP packet, protocol %d\n",
			    ntohs(fh->fip_proto));
		return -1;
	}

	switch (fh->fip_subcode) {
	case FIP_VLAN_REQ:
		FCM_LOG_DBG("received VLAN req, subcode=%d\n", fh->fip_subcode);
		rc = fip_recv_vlan_req(sa, p);
		break;
	default:
		FCM_LOG_DBG("ignored FIP VLAN packet with subcode %d\n",
			    fh->fip_subcode);
		break;
	}
	return rc;
}

static void fip_responder(void *arg)
{
	struct fcoe_port *p = arg;

	fip_recv(p->fip_responder_socket, fip_vlan_disc_handler, p);
}

static void init_fip_vn2vn_responder(struct fcoe_port *p)
{
	int s;

	if (p->fip_responder_socket >= 0)
		return;

	s = fip_socket(p->ifindex, FIP_ALL_VN2VN);
	if (s < 0) {
		FCM_LOG_ERR(errno, "%s: Failed to get fip socket\n", p->ifname);
		return;
	}
	p->fip_responder_socket = s;
	FCM_LOG_DBG("%s: Adding FIP responder socket\n", p->ifname);
	sa_select_add_fd(s, fip_responder, NULL, NULL, p);
}

static void update_fcoe_port_state(struct fcoe_port *p, unsigned int type,
				   u_int8_t operstate, enum fcoeport_ifname t)
{
	struct fcm_netif *ff = NULL;

	if (type != RTM_DELLINK) {
		ff = fcm_netif_lookup_create(p->real_ifname);
		if (!ff)
			return;

		/* Only set the ff_operstate field of the network interface
		 * element if this routine is being called for the real
		 * network interface, or, if the interface is a VLAN, if the
		 * network interface element has not been intialized and the
		 * VLAN operstate is up (if VLAN is up, then real interface is
		 * up).
		 */
		if ((t == FCP_REAL_IFNAME) ||
		    ((t == FCP_CFG_IFNAME) &&
		     (ff->ff_operstate == IF_OPER_UNKNOWN) &&
		     (operstate == IF_OPER_UP)))
			ff->ff_operstate = operstate;

		if (t == FCP_REAL_IFNAME && p->fip_resp)
			init_fip_vn2vn_responder(p);

		if (!p->fcoe_enable) {
			fcp_set_next_action(p, FCP_DESTROY_IF);
			return;
		}

		if (operstate == IF_OPER_UP) {
			if (p->dcb_required) {
				/* If DCB is required, do not start the dcbd
				 * query sequence if this routine is being
				 * called for a real interface and the FCoE
				 * interface is configured on a VLAN.
				 */
				if (!((t == FCP_REAL_IFNAME) &&
				      strncmp(p->ifname, p->real_ifname,
					      IFNAMSIZ))) {
					fcm_dcbd_state_set(ff,
							   FCD_GET_DCB_STATE);
					ieee_state_set(ff, IEEE_GET_STATE);
				}
			} else {
				/* hold off on auto-created VLAN ports until
				 * VLAN discovery can validate that the setup
				 * has not changed */
				if (!p->auto_created || !p->auto_vlan)
					fcp_set_next_action(p, FCP_ACTIVATE_IF);
			}
		} else {
			fcp_set_next_action(p, FCP_DISABLE_IF);
		}
	} else {
		fcp_set_next_action(p, FCP_DESTROY_IF);
	}
}

static int fcoe_vid_from_ifname(const char *ifname)
{
	int vid = -1;
	int rc;

	if (strlen(ifname) <= strlen(FCOE_VLAN_SUFFIX) ||
	    strcmp(&ifname[strlen(ifname) - strlen(FCOE_VLAN_SUFFIX)],
	           FCOE_VLAN_SUFFIX))
		return vid;
	rc = sscanf(ifname, FCOE_VID_SCAN_FORMAT, &vid);
	if (rc == 1)
		return vid;
	return -1;
}

static void fcm_process_link_msg(struct ifinfomsg *ip, int len, unsigned type)
{
	struct fcoe_port *p;
	struct rtattr *ap;
	char ifname[IFNAMSIZ];
	char real_dev[IFNAMSIZ];
	u_int8_t operstate;
	unsigned char mac[ETHER_ADDR_LEN];
	int is_vlan;
	int ifindex;

	is_vlan = 0;
	operstate = IF_OPER_UNKNOWN;

	ifindex = ip->ifi_index;

	if (ip->ifi_type != ARPHRD_ETHER)
		return;

	len -= sizeof(*ip);
	for (ap = (struct rtattr *)(ip + 1); RTA_OK(ap, len);
	     ap = RTA_NEXT(ap, len)) {
		switch (ap->rta_type) {
		case IFLA_ADDRESS:
			if (RTA_PAYLOAD(ap) == 6)
				memcpy(mac, RTA_DATA(ap), ETHER_ADDR_LEN);
			break;

		case IFLA_IFNAME:
			sa_strncpy_safe(ifname, sizeof(ifname),
					RTA_DATA(ap),
					RTA_PAYLOAD(ap));
			break;

		case IFLA_OPERSTATE:
			operstate = *(uint8_t *) RTA_DATA(ap);
			break;

		case IFLA_LINKINFO:
			if (fcm_is_linkinfo_vlan(ap))
				is_vlan = 1;
			break;

		default:
			break;
		}
	}

	p = fcm_find_fcoe_port(ifname, FCP_CFG_IFNAME);
	if (is_vlan) {
		/* if not in fcoe port list, then ignore this ifname */
		if (!p)
			return;

		p->ifindex = ifindex;
		memcpy(p->mac, mac, ETHER_ADDR_LEN);

		/* don't do VLAN discovery on a VLAN */
		p->auto_vlan = 0;

		/* try to find the real device name */
		real_dev[0] = '\0';
		fcm_vlan_dev_real_dev(ifname, real_dev);
		if (strlen(real_dev))
			strncpy(p->real_ifname, real_dev, strlen(real_dev)+1);
		if (p->ready)
			update_fcoe_port_state(p, type, operstate,
					       FCP_CFG_IFNAME);
		p->last_msg_type = type;
	} else {
		/* the ifname is not a VLAN.  handle the case where it has
		 * an FCoE interface configured on it.
		 */
		if (p) {
			p->ifindex = ifindex;
			memcpy(p->mac, mac, ETHER_ADDR_LEN);
			strncpy(p->real_ifname, ifname, strlen(ifname)+1);
			update_fcoe_port_state(p, type, operstate,
					       FCP_REAL_IFNAME);
		}

		/* handle all FCoE ports which are on VLANs over this
		 * ifname.
		 */
		p = fcm_find_fcoe_port(ifname, FCP_REAL_IFNAME);
		for (; p; p = fcm_find_next_fcoe_port(p, ifname)) {
			int vid;

			if (!p->ready)
				continue;
			vid = fcoe_vid_from_ifname(p->ifname);
			if (vid >= 0 && p->mode == FCOE_MODE_VN2VN) {
				struct fcoe_port *vp;

				vp = fcm_new_vlan(ifindex, vid, true);
				vp->dcb_required = p->dcb_required;
			}
			update_fcoe_port_state(p, type, operstate,
					       FCP_REAL_IFNAME);
		}
	}
}

static struct rtattr *find_nested_attr(struct rtattr *rta, __u16 type)
{
	struct rtattr *rta_child;

	rta_child = NLA_DATA(rta);
	rta = NLA_NEXT(rta);

	for (; rta > rta_child; rta_child = NLA_NEXT(rta_child))
		if (rta_child->rta_type == type)
			return rta_child;

	return NULL;
}

static struct rtattr *find_attr(struct nlmsghdr *nlh, __u16 type)
{
	struct rtattr *rta;
	int len;

	rta = (struct rtattr *)(((char *)NLMSG_DATA(nlh)) +
		NLMSG_ALIGN(sizeof(struct dcbmsg)));
	len = NLMSG_PAYLOAD(nlh, 0) - sizeof(struct dcbmsg);

	while (RTA_OK(rta, len)) {
		if (rta->rta_type == type)
			return rta;

		rta = RTA_NEXT(rta, len);
	}

	return NULL;
}

static int ieee_get_dcbx(struct nlmsghdr *nlh)
{
	struct rtattr *rta;

	rta = find_attr(nlh, DCB_ATTR_DCBX);
	if (!rta)
		return -EIO;

	return *(__u8 *)NLA_DATA(rta);
}

static int get_pri_mask_from_ieee(struct rtattr *rta, __u8 dcbx_cap)
{
	struct rtattr *rta_parent;
	struct rtattr *rta_child;
	int rval;
	__u8 ieee = dcbx_cap & DCB_CAP_DCBX_VER_IEEE;

	rta_parent = find_nested_attr(rta, DCB_ATTR_IEEE_APP_TABLE);
	if (!rta_parent)
		return -EIO;

	rta_child = NLA_DATA(rta_parent);
	rta_parent = NLA_NEXT(rta_parent);

	rval = 0;
	for (; rta_parent > rta_child; rta_child = NLA_NEXT(rta_child)) {
		struct dcb_app *app;

		if (rta_child->rta_type != DCB_ATTR_IEEE_APP)
			continue;

		app = (struct dcb_app *)NLA_DATA(rta_child);
		if (app->protocol != FCOE_ETH_TYPE)
			continue;

		if (ieee) {
			if (app->selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE)
				rval |= 1 << app->priority;
		} else {
			if (app->selector == DCB_APP_IDTYPE_ETHTYPE)
				return app->priority;
		}
	}

	return rval;
}

static void fcm_process_ieee_msg(struct nlmsghdr *nlh)
{
	struct dcbmsg *d;
	struct rtattr *rta_parent;
	struct rtattr *rta_child;
	struct fcm_netif *ff;
	int dcbx_cap;
	int pri_mask;
	char ifname[IFNAMSIZ];

	d = (struct dcbmsg *)NLMSG_DATA(nlh);
	if (d->cmd != DCB_CMD_IEEE_GET && d->cmd != DCB_CMD_IEEE_SET) {
		FCM_LOG_DBG("Unexpected command type %d\n", d->cmd);
		return;
	}

	rta_parent = (struct rtattr *)(((char *)d) + NLMSG_ALIGN(sizeof(*d)));
	if (rta_parent->rta_type != DCB_ATTR_IFNAME)
		return;

	strncpy(ifname, NLA_DATA(rta_parent), sizeof(ifname));
	ff = fcm_netif_lookup_create(ifname);
	if (!ff) {
		FCM_LOG("Processing IEEE message: %s not found or created\n",
			ifname);
		return;
	}

	dcbx_cap = ieee_get_dcbx(nlh);
	if (dcbx_cap < 0) {
		FCM_LOG("Processing IEEE message: No DCBx capabilities on %s\n",
			ifname);
		return;
	}
	ff->dcbx_cap = dcbx_cap;
	if (!ff->ff_dcb_state)
		ff->ff_dcb_state = !!(dcbx_cap & DCB_CAP_DCBX_VER_IEEE);
	if (d->cmd == DCB_CMD_IEEE_SET && !(dcbx_cap & DCB_CAP_DCBX_VER_IEEE)) {
		FCM_LOG("Processing IEEE messgae: %s not in IEEE mode\n",
			ifname);
	}

	rta_parent = find_attr(nlh, DCB_ATTR_IEEE);
	if (!rta_parent) {
		FCM_LOG("Processing IEEE message: No DCB attribute found on %s\n",
			ifname);
		return;
	}

	rta_child = find_nested_attr(rta_parent, DCB_ATTR_IEEE_PFC);
	if (!rta_child) {
		FCM_LOG("Processing IEEE messgae: No PFC attribute found on %s\n",
			ifname);
		return;
	}

	struct ieee_pfc *ieee_pfc = (struct ieee_pfc *)NLA_DATA(rta_child);

	ff->ieee_pfc_info = ieee_pfc->pfc_en;

	pri_mask = get_pri_mask_from_ieee(rta_parent, dcbx_cap);
	if (pri_mask < 0) {
		FCM_LOG("Processing IEEE message: No Priority mask found on %s\n",
			ifname);
		return;
	}
	FCM_LOG_DBG("Processing IEEE message: FCoE Priority mask on %s is 0x%02X\n",
		    ifname, pri_mask);
	ff->ieee_app_info = pri_mask;

	if (ff->ieee_state == IEEE_GET_STATE && d->cmd == DCB_CMD_IEEE_GET &&
	    ff->ieee_resp_pending == nlh->nlmsg_seq)
		ieee_state_set(ff, IEEE_DONE);
}

static void fcm_link_recv(UNUSED void *arg)
{
	int rc;
	char *buf;
	struct nlmsghdr *hp;
	struct ifinfomsg *ip;
	unsigned type;
	size_t plen;
	size_t rlen;

	buf = fcm_link_buf;
	rc = read(fcm_link_socket, buf, fcm_link_buf_size);
	if (rc <= 0) {
		if (rc < 0)
			FCM_LOG_ERR(errno, "Error reading from "
				    "netlink socket with fd %d",
				    fcm_link_socket);
		return;
	}

	if (fcm_link_buf_check(rc)) {
		fcm_link_getlink();
		return;
	}

	hp = (struct nlmsghdr *)buf;
	rlen = rc;
	for (hp = (struct nlmsghdr *)buf; NLMSG_OK(hp, rlen);
	     hp = NLMSG_NEXT(hp, rlen)) {

		type = hp->nlmsg_type;
		if (hp->nlmsg_type == NLMSG_DONE)
			break;

		if (hp->nlmsg_type == NLMSG_ERROR) {
			log_nlmsg_error(hp, rlen, "nlmsg error");
			break;
		}

		plen = NLMSG_PAYLOAD(hp, 0);
		ip = (struct ifinfomsg *)NLMSG_DATA(hp);
		if (plen < sizeof(*ip)) {
			FCM_LOG("too short (%d) to be a LINK message", rc);
			break;
		}

		switch (type) {
		case RTM_NEWLINK:
		case RTM_DELLINK:
		case RTM_GETLINK:
			FCM_LOG_DBG("Link event: %d flags %05X index %d ",
				    type, ip->ifi_flags, ip->ifi_index);

			fcm_process_link_msg(ip, plen, type);
			break;

		case RTM_GETDCB:
		case RTM_SETDCB:
			fcm_process_ieee_msg(hp);
			break;

		default:
			FCM_LOG_DBG("%s: Unexpected type %d\n", __func__, type);
			break;
		}
	}
}

/*
 * Send rt_netlink request for all network interfaces.
 */
static void fcm_link_getlink(void)
{
	struct {
		struct nlmsghdr nl;
		struct ifinfomsg ifi;	/* link level specific information,
					   not dependent on network protocol */
	} msg;

	int rc;

	memset(&msg, 0, sizeof(msg));
	msg.nl.nlmsg_len = sizeof(msg);
	msg.nl.nlmsg_type = RTM_GETLINK;
	msg.nl.nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT | NLM_F_ATOMIC;
	msg.nl.nlmsg_seq = ++fcm_link_seq;
	msg.nl.nlmsg_pid = getpid();
	msg.ifi.ifi_family = AF_UNSPEC;
	msg.ifi.ifi_type = ARPHRD_ETHER;
	rc = write(fcm_link_socket, &msg, sizeof(msg));
	if (rc < 0)
		FCM_LOG_ERR(errno, "write error");
}

/*
 * Check for whether buffer needs to grow based on amount read.
 * Free's the old buffer so don't use that after this returns non-zero.
 */
static int fcm_link_buf_check(size_t read_len)
{
	char *buf;
	size_t len = read_len;

	if (len > fcm_link_buf_size - fcm_link_buf_fuzz) {
		len = fcm_link_buf_size;
		len = len + len / 2;	/* grow by 50% */
		buf = malloc(len);
		if (buf != NULL) {
			free(fcm_link_buf);
			fcm_link_buf = buf;
			fcm_link_buf_size = len;
			return 1;
		} else {
			FCM_LOG_ERR(errno, "failed to allocate link buffer");
		}
	}
	return 0;
}

static void fcm_fcoe_init(void)
{
	if (fcm_read_config_files())
		exit(1);

	if (force_legacy || access(FCOE_BUS_CREATE, F_OK)) {
		FCM_LOG_DBG("Using libfcoe module parameter interfaces\n");
		libfcoe_control = &libfcoe_module_tmpl;
	} else {
		FCM_LOG_DBG("Using /sys/bus/fcoe interfaces\n");
		libfcoe_control = &libfcoe_bus_tmpl;
	}
}

/*
 * Allocate an FCoE interface state structure.
 */
static struct fcm_netif *fcm_netif_alloc(char *ifname)
{
	struct fcm_netif *ff;

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	ff = calloc(1, sizeof(*ff));
	if (ff) {
		snprintf(ff->ifname, sizeof(ff->ifname), "%s", ifname);
		ff->ff_operstate = IF_OPER_UNKNOWN;
		ff->ff_enabled = 1;
		TAILQ_INSERT_TAIL(&fcm_netif_head, ff, ff_list);
	} else {
		FCM_LOG_ERR(errno, "failed to allocate fcm_netif");
	}

	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);

	return ff;
}

/*
 * Find or create an FCoE network interface by ifname.
 * @ifname - interface name to create
 *
 * This creates a netif interface structure with interface name,
 * or if one already exists returns the existing one.
 */
static struct fcm_netif *fcm_netif_lookup_create(char *ifname)
{
	struct fcm_netif *ff;

	TAILQ_FOREACH(ff, &fcm_netif_head, ff_list) {
		if (!strncmp(ifname, ff->ifname, IFNAMSIZ))
			return ff;
	}

	ff = fcm_netif_alloc(ifname);
	if (ff != NULL) {
		sa_timer_init(&ff->dcbd_retry_timer, fcm_dcbd_retry_timeout,
			      (void *)ff);
		FCM_LOG_DEV_DBG(ff, "Monitoring port %s\n", ifname);
	}

	return ff;
}

/*
 * Find an FCoE interface by name.
 */
static struct fcm_netif *fcm_netif_lookup(char *ifname)
{
	struct fcm_netif *curr, *ff = NULL;

	TAILQ_FOREACH(curr, &fcm_netif_head, ff_list) {
		if (strcmp(curr->ifname, ifname) == 0) {
			ff = curr;
			break;
		}
	}

	return ff;
}

static void fcm_dcbd_init()
{
	fcm_clif->cl_fd = -1;	/* not connected */
	fcm_clif->cl_ping_pending = 0;
	sa_timer_init(&fcm_dcbd_timer, fcm_dcbd_timeout, NULL);
	fcm_dcbd_timeout(NULL);
}

static int fcm_dcbd_connect(void)
{
	int rc;
	int fd;
	struct sockaddr_un dest;
	struct sockaddr_un *lp;
	socklen_t addrlen;

	ASSERT(fcm_clif->cl_fd < 0);
	fd = socket(PF_UNIX, SOCK_DGRAM, 0);
	if (fd < 0) {
		FCM_LOG_ERR(errno, "clif socket open failed");	/* XXX */
		return 0;
	}

	lp = &fcm_clif->cl_local;
	memset(lp, 0, sizeof(*lp));
	lp->sun_family = AF_LOCAL;
	lp->sun_path[0] = '\0';
	rc = bind(fd, (struct sockaddr *)lp, sizeof(sa_family_t));
	if (rc < 0) {
		FCM_LOG_ERR(errno, "clif bind failed");
		close(fd);
		return 0;
	}

	memset(&dest, 0, sizeof(dest));
	dest.sun_family = AF_LOCAL;
	dest.sun_path[0] = '\0';
	snprintf(&dest.sun_path[1], sizeof(dest.sun_path) - 1,
		 "%s", LLDP_CLIF_SOCK);
	addrlen = sizeof(sa_family_t) + strlen(dest.sun_path + 1) + 1;
	rc = connect(fd, (struct sockaddr *)&dest, addrlen);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "Failed to connect to lldpad");
		close(fd);
		return 0;
	}
	fcm_clif->cl_fd = fd;
	sa_select_add_fd(fd, fcm_dcbd_rx, NULL, NULL, fcm_clif);
	FCM_LOG_DBG("connected to lldpad");
	return 1;
}

static void fcm_dcbd_timeout(UNUSED void *arg)
{
	if (fcm_clif->cl_ping_pending > 0) {
		fcm_dcbd_request("D");	/* DETACH_CMD */
		fcm_dcbd_disconnect();
	}
	if (fcm_clif->cl_fd < 0) {
		if (fcm_dcbd_connect())
			fcm_dcbd_request("A");	/* ATTACH_CMD: for events */
		else
			sa_timer_set(&fcm_dcbd_timer, DCBD_CONNECT_TIMEOUT);
	} else {
		fcm_clif->cl_ping_pending++;
		fcm_dcbd_request("P");	/* ping to verify connection */
	}
}

static void fcm_dcbd_retry_timeout(void *arg)
{
	struct fcm_netif *ff = (struct fcm_netif *)arg;

	ASSERT(ff);
	FCM_LOG_DBG("%s: lldpad retry TIMEOUT occurred [%d]",
		    ff->ifname, ff->dcbd_retry_cnt);

	fcm_dcbd_state_set(ff, FCD_GET_DCB_STATE);
	fcm_netif_advance(ff);
}

static void fcm_dcbd_disconnect(void)
{
	if (fcm_clif) {
		if (fcm_clif->cl_fd >= 0) {
			sa_select_rem_fd(fcm_clif->cl_fd);
			close(fcm_clif->cl_fd);
		}
		fcm_clif->cl_fd = -1;	/* mark as disconnected */
		fcm_clif->cl_busy = 0;
		fcm_clif->cl_ping_pending = 0;
		FCM_LOG_DBG("Disconnected from lldpad");
	}
}

static void fcm_dcbd_shutdown(void)
{
	FCM_LOG_DBG("Shutdown lldpad connection\n");
	fcm_dcbd_request("D");	/* DETACH_CMD */
	fcm_dcbd_disconnect();
	closelog();
}

static void fcm_cleanup(void)
{
	struct fcoe_port *curr, *next;
	struct fcm_netif *ff, *head;

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	for (curr = fcoe_config.port; curr; curr = next) {
		FCM_LOG_DBG("OP: DESTROY %s\n", curr->ifname);
		fcm_fcoe_if_action(FCOE_DESTROY,  curr->ifname);
		next = curr->next;
		free(curr);
	}

	for (head = TAILQ_FIRST(&fcm_netif_head); head; head = ff) {
		ff = TAILQ_NEXT(head, ff_list);
		TAILQ_REMOVE(&fcm_netif_head, head, ff_list);
		free(head);
	}

	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);

	free(fcm_link_buf);
}

static u_int32_t fcm_get_hex(char *cp, u_int32_t len, char **endptr)
{
	u_int32_t hex = 0;

	while (len > 0) {
		len--;
		if (*cp >= '0' && *cp <= '9')
			hex = (hex << 4) | (*cp - '0');
		else if (*cp >= 'A' && *cp <= 'F')
			hex = (hex << 4) | (*cp - 'A' + 10);
		else if (*cp >= 'a' && *cp <= 'f')
			hex = (hex << 4) | (*cp - 'a' + 10);
		else
			break;
		cp++;
	}
	*endptr = (len == 0) ? NULL : cp;
	return hex;
}

static void fcm_dcbd_rx(void *arg)
{
	struct fcm_clif *clif = arg;
	cmd_status st;
	char buf[128];
	size_t len;
	int rc;
	char *ep;

	len = sizeof(buf);
	rc = read(clif->cl_fd, buf, sizeof(buf) - 1);
	if (rc < 0)
		FCM_LOG_ERR(errno, "read");
	else if (rc > 0 && rc < (int)sizeof(buf)) {
		buf[rc] = '\0';
		len = strlen(buf);
		ASSERT(len <= rc);

		switch (buf[CLIF_RSP_MSG_OFF]) {
		case CMD_RESPONSE:
			st = fcm_get_hex(buf + CLIF_STAT_OFF, CLIF_STAT_LEN,
					 &ep);
			if (ep != NULL)
				FCM_LOG("unexpected response code from lldpad: "
					"len %zd buf %s rc %d", len, buf, rc);
			else if (st != cmd_success &&
				 st != cmd_not_applicable &&
				 st != cmd_device_not_found) {
				FCM_LOG("error response from lldpad: "
					"error %d len %zd %s",
					st, len, buf);
			}
			fcm_clif->cl_busy = 0;

			switch (buf[3]) {
			case DCB_CMD:
				fcm_dcbd_cmd_resp(buf, st);
				break;
			case ATTACH_CMD:
				break;
			case DETACH_CMD:
				break;
			case PING_CMD:
				if (clif->cl_ping_pending > 0)
					--clif->cl_ping_pending;
				break;
			case LEVEL_CMD:
				break;
			default:
				FCM_LOG("Unexpected cmd in response "
					"from lldpad: len %zd %s",
					len, buf);
				break;
			}
			break;

		case EVENT_MSG:
			fcm_dcbd_event(buf, len);
			break;
		default:
			FCM_LOG("Unexpected message from lldpad: len %zd buf %s",
				len, buf);
			break;
		}
	}
}

/*
 * returns:  1 if request was successfully written
 *           0 if the write failed
 */
static int fcm_dcbd_request(char *req)
{
	size_t len;
	int rc;

	if (fcm_clif->cl_fd < 0)
		return 0;
	len = strlen(req);
	ASSERT(fcm_clif->cl_busy == 0);
	sa_timer_set(&fcm_dcbd_timer, DCBD_CONNECT_TIMEOUT);
	fcm_clif->cl_busy = 1;
	rc = write(fcm_clif->cl_fd, req, len);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "Failed write req %s len %zd", req, len);
		fcm_clif->cl_busy = 0;
		fcm_dcbd_disconnect();
		sa_timer_set(&fcm_dcbd_timer, DCBD_CONNECT_RETRY_TIMEOUT);
		return 0;
	}

	if (rc > FCM_PING_REQ_LEN)
		FCM_LOG_DBG("sent '%s', rc=%d bytes succeeded", req, rc);
	return 1;
}

/*
 * Find port for message.
 * The port name length starts at len_off for len_len bytes.
 * The entire message length is len.
 * The pointer to the message pointer is passed in, and updated to point
 * past the interface name.
 */
static struct fcm_netif *fcm_dcbd_get_port(char **msgp, size_t len_off,
					   size_t len_len, size_t len)
{
	struct fcm_netif *ff;
	u_int32_t if_len;
	char *ep;
	char *msg;
	char ifname[IFNAMSIZ];

	msg = *msgp;
	if (len_off + len_len >= len)
		return NULL;

	if_len = fcm_get_hex(msg + len_off, len_len, &ep);
	if (ep != NULL) {
		FCM_LOG("Parse error on port len: msg %s", msg);
		return NULL;
	}

	if (len_off + len_len + if_len > len) {
		FCM_LOG("Invalid port len %d msg %s", if_len, msg);
		return NULL;
	}
	msg += len_off + len_len;
	sa_strncpy_safe(ifname, sizeof(ifname), msg, if_len);
	*msgp = msg + if_len;
	ff = fcm_netif_lookup(ifname);
	if (ff == NULL) {
		FCM_LOG("ifname '%s' not found", ifname);
	}
	return ff;
}

/*
 * (XXX) Notes:
 * This routine is here to help fcm_dcbd_cmd_resp() to pick up
 * information of the response packet from the DCBD.
 * Returns:  0 on success
 *          -1 on failure
 */
static int dcb_rsp_parser(struct fcm_netif *ff, char *rsp)
{
	int version;
	int dcb_cmd;
	int feature;
	int subtype;
	int plen;
	int doff;
	int i;
	int n;
	struct feature_info *f_info = NULL;
	char buf[20];

	feature = hex2int(rsp+DCB_FEATURE_OFF);

	dcb_cmd = hex2int(rsp+DCB_CMD_OFF);

	version = rsp[DCB_VER_OFF] & 0x0f;
	if (version != CLIF_MSG_VERSION) {
		FCM_LOG_DEV(ff, "WARNING: Unexpected rsp version %d\n",
			    version);
		return -1;
	}

	subtype = hex2int(rsp+DCB_SUBTYPE_OFF);
	plen = hex2int(rsp+DCB_PORTLEN_OFF);
	doff = DCB_PORT_OFF + plen;

	switch (feature) {
	case FEATURE_DCB:
		ff->ff_dcb_state = (*(rsp+doff+CFG_ENABLE) == '1');
		return 0;
	case FEATURE_PFC:
		f_info = &ff->ff_pfc_info;
		break;
	case FEATURE_APP:
		f_info = &ff->ff_app_info;
		f_info->subtype = subtype;
		break;
	default:
		return -1;
	}

	switch (dcb_cmd) {
	case CMD_GET_CONFIG:
		f_info->enable = (*(rsp+doff+CFG_ENABLE) == '1');
		f_info->advertise = (*(rsp+doff+CFG_ADVERTISE) == '1');
		f_info->willing = (*(rsp+doff+CFG_WILLING) == '1');
		doff += CFG_LEN;
		break;

	case CMD_GET_OPER:
		f_info->op_vers = hex2int(rsp+doff+OPER_OPER_VER);
		f_info->op_error = hex2int(rsp+doff+OPER_ERROR);
		f_info->op_mode = (*(rsp+doff+OPER_OPER_MODE) == '1');
		f_info->syncd = (*(rsp+doff+OPER_SYNCD) == '1');
		doff += OPER_LEN;
		if (feature == FEATURE_PFC) {
			f_info->u.pfcup = 0;
			for (i = 0; i < MAX_USER_PRIORITIES; i++) {
				if (*(rsp+doff+PFC_UP(i)) == '1')
					f_info->u.pfcup |= 1<<i;
			}
		}
		if (feature == FEATURE_APP && subtype == APP_FCOE_STYPE) {
			n = hex2int(rsp+doff+APP_LEN);
			snprintf(buf, sizeof(buf), "%*.*s\n",
				 n, n, rsp+doff+APP_DATA);
			f_info->u.appcfg = hex2int(buf);
		}
		break;
	}

	return 0;
}

/*
 * validate_ieee_info - Validation IEEE DCB status for FCoE
 *
 * Returns:  FCP_ACTIVATE_IF - if the dcb netif qualifies for an fcoe interface
 *           FCP_DESTROY_IF - if the dcb netif should not support fcoe interface
 *           FCP_ERROR - if dcb configuration has errors
 *           FCP_WAIT - if dcb criteria is inconclusive
 */
static enum fcp_action validate_ieee_info(struct fcm_netif *ff)
{
	if (ff->ieee_pfc_info & ff->ieee_app_info) {
		FCM_LOG_DBG("%s: %s: IEEE active and valid\n",
			__func__, ff->ifname);
		return FCP_ACTIVATE_IF;
	}
	FCM_LOG_DBG("%s: %s: IEEE active and invalid, pfc=0x%x, app=0x%x\n",
		    __func__, ff->ifname, ff->ieee_pfc_info, ff->ieee_app_info);
	return FCP_WAIT;
}

/*
 * validate_dcbd_info - Validating DCBD configuration and status
 *
 * Returns:  FCP_ACTIVATE_IF - if the dcb netif qualifies for an fcoe interface
 *           FCP_DESTROY_IF - if the dcb netif should not support fcoe interface
 *           FCP_ERROR - if dcb configuration has errors
 *           FCP_WAIT - if dcb criteria is inconclusive
 */
static enum fcp_action validate_dcbd_info(struct fcm_netif *ff)
{
	int errors = 0;
	int dcbon;

	dcbon = ff->ff_dcb_state;
	if (dcbon && (ff->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
		return validate_ieee_info(ff);

	/* check if dcb state qualifies to create the fcoe interface */
	if (dcbon &&
	    ff->ff_app_info.enable &&
	    ff->ff_pfc_info.enable &&
	    ff->ff_app_info.op_mode &&
	    ff->ff_pfc_info.op_mode &&
	    ff->ff_pfc_info.u.pfcup & ff->ff_app_info.u.appcfg) {

		if (dcbon && !ff->ff_app_info.willing) {
			FCM_LOG_DEV(ff,
				    "WARNING: FCoE willing mode is false\n");
			errors++;
		}
		if (dcbon && !ff->ff_app_info.advertise) {
			FCM_LOG_DEV(ff,
				    "WARNING: FCoE advertise mode is false\n");
			errors++;
		}
		if (dcbon && !ff->ff_pfc_info.willing) {
			FCM_LOG_DEV(ff,
				    "WARNING: PFC willing mode is false\n");
			errors++;
		}
		if (dcbon && !ff->ff_pfc_info.advertise) {
			FCM_LOG_DEV(ff,
				    "WARNING: PFC advertise mode is false\n");
			errors++;
		}

		if (errors)
			FCM_LOG_DEV_DBG(ff,
					"WARNING: DCB may not be configured correctly\n");
		else
			FCM_LOG_DEV_DBG(ff, "DCB is configured correctly\n");


		if (ff->ff_enabled)
			return FCP_ACTIVATE_IF;
		else {
			ff->ff_enabled = 1;
			return FCP_ENABLE_IF;
		}
	}

	/* check if dcb state qualifies to destroy the fcoe interface */
	if (!dcbon ||
	    !ff->ff_app_info.enable ||
	    (ff->ff_app_info.op_mode && ff->ff_pfc_info.op_mode &&
	     !(ff->ff_pfc_info.u.pfcup & ff->ff_app_info.u.appcfg))) {

		if (dcbon && !ff->ff_dcb_state)
			FCM_LOG_DEV(ff, "WARNING: DCB is disabled\n");

		if (dcbon && !ff->ff_app_info.enable)
			FCM_LOG_DEV(ff, "WARNING: FCoE enable is off\n");

		if (dcbon &&
		    !(ff->ff_pfc_info.u.pfcup & ff->ff_app_info.u.appcfg))
			FCM_LOG_DEV(ff,
				    "WARNING: FCoE priority (0x%02x) doesn't "
				    "intersect with PFC priority (0x%02x)\n",
				    ff->ff_app_info.u.appcfg,
				    ff->ff_pfc_info.u.pfcup);

		ff->ff_enabled = 0;
		return FCP_DISABLE_IF;
	}

	/* The dcbd state does not match the create or destroy criteria.
	 * Log possible problems.
	 */
	if (dcbon && !ff->ff_app_info.willing) {
		FCM_LOG_DEV(ff, "WARNING: FCoE willing mode is false\n");
		errors++;
	}
	if (dcbon && !ff->ff_app_info.advertise) {
		FCM_LOG_DEV(ff, "WARNING: FCoE advertise mode is false\n");
		errors++;
	}
	if (dcbon && !ff->ff_app_info.op_mode) {
		FCM_LOG_DEV(ff, "WARNING: FCoE operational mode is false\n");
		print_errors(ff->ff_app_info.op_error);
		errors++;
	}
	if (dcbon && !ff->ff_pfc_info.enable) {
		FCM_LOG_DEV(ff, "WARNING: PFC enable is off\n");
		errors++;
	}
	if (dcbon && !ff->ff_pfc_info.advertise) {
		FCM_LOG_DEV(ff, "WARNING: PFC advertise mode is false\n");
		errors++;
	}
	if (dcbon && !ff->ff_app_info.op_mode) {
		FCM_LOG_DEV(ff, "WARNING: APP:0 operational mode is false\n");
		print_errors(ff->ff_app_info.op_error);
		errors++;
	}
	if (dcbon && !ff->ff_pfc_info.op_mode) {
		FCM_LOG_DEV(ff, "WARNING: PFC operational mode is false\n");
		print_errors(ff->ff_pfc_info.op_error);
		errors++;
	}
	if (dcbon && !(ff->ff_pfc_info.u.pfcup & ff->ff_app_info.u.appcfg)) {
		FCM_LOG_DEV(ff, "WARNING: APP:0 priority (0x%02x) doesn't "
			    "intersect with PFC priority (0x%02x)\n",
			    ff->ff_app_info.u.appcfg,
			    ff->ff_pfc_info.u.pfcup);
		errors++;
	}
	if (errors) {
		FCM_LOG_DEV(ff, "WARNING: DCB may be configured incorrectly\n");
		return FCP_ERROR;
	}

	return FCP_WAIT;
}

/*
 * clear_dcbd_info - clear dcbd info to unknown values
 *
 */
static void clear_dcbd_info(struct fcm_netif *ff)
{
	memset(&ff->ff_pfc_info, 0, sizeof(struct feature_info));
	memset(&ff->ff_app_info, 0, sizeof(struct feature_info));
}


/**
 * fcm_dcbd_set_config() - Response handler for set config command
 * @ff: fcoe port structure
 * @st: status
 */
static void fcm_dcbd_set_config(struct fcm_netif *ff)
{
	if (ff->ff_dcbd_state == FCD_SEND_CONF)
		fcm_dcbd_state_set(ff, FCD_GET_PFC_CONFIG);
}

/**
 * fcm_dcbd_get_config() - Response handler for get config command
 * @ff:   fcoe port structure
 * @resp: response buffer
 * @st:   status
 */
static void fcm_dcbd_get_config(struct fcm_netif *ff, char *resp)
{
	switch (ff->ff_dcbd_state) {
	case FCD_GET_DCB_STATE:
		if (!dcb_rsp_parser(ff, resp)) {
			if (ff->ff_dcb_state &&
			    !(ff->dcbx_cap & DCB_CAP_DCBX_VER_IEEE))
				fcm_dcbd_state_set(ff, FCD_GET_PFC_CONFIG);
			else
				fcm_dcbd_state_set(ff, FCD_DONE);
		} else
			fcm_dcbd_state_set(ff, FCD_ERROR);
		break;
	case FCD_GET_PFC_CONFIG:
		if (!dcb_rsp_parser(ff, resp))
			fcm_dcbd_state_set(ff, FCD_GET_APP_CONFIG);
		else
			fcm_dcbd_state_set(ff, FCD_ERROR);
		break;
	case FCD_GET_APP_CONFIG:
		if (!dcb_rsp_parser(ff, resp))
			fcm_dcbd_state_set(ff, FCD_GET_PFC_OPER);
		else
			fcm_dcbd_state_set(ff, FCD_ERROR);
		break;
	default:
		break;
	}
}


/**
 * fcm_dcbd_get_oper() - Response handler for get operational state command
 * @ff:   fcoe port structure
 * @resp: response buffer
 * @cp:   response buffer pointer, points past the interface name
 * @st:   status
 *
 * Sample msg: R00C103050004eth8010100100208
 *                  opppssll    vvmmeemsllpp
 */
static void fcm_dcbd_get_oper(struct fcm_netif *ff, char *resp, char *cp)
{
	u_int32_t val;
	char *ep = NULL;

	val = fcm_get_hex(cp + OPER_ERROR, 2, &ep);

	if (ep) {
		FCM_LOG_DEV(ff, "Invalid get oper response "
			    "parse error byte %td, resp %s", ep - cp, cp);
		fcm_dcbd_state_set(ff, FCD_ERROR);
	} else {
		if (val && fcoe_config.debug)
			print_errors(val);

		switch (ff->ff_dcbd_state) {
		case FCD_GET_PFC_OPER:
			if (dcb_rsp_parser(ff, resp) || !ff->ff_pfc_info.syncd)
				fcm_dcbd_state_set(ff, FCD_ERROR);
			else
				fcm_dcbd_state_set(ff, FCD_GET_APP_OPER);

			FCM_LOG_DEV_DBG(ff, "PFC feature is %ssynced",
					ff->ff_pfc_info.syncd ? "" : "not ");
			FCM_LOG_DEV_DBG(ff, "PFC operating mode is %s",
					ff->ff_pfc_info.op_mode ? "on" :
					"off ");
			break;

		case FCD_GET_APP_OPER:
			if (dcb_rsp_parser(ff, resp) || !ff->ff_app_info.syncd)
				fcm_dcbd_state_set(ff, FCD_ERROR);
			else
				fcm_dcbd_state_set(ff, FCD_DONE);

			FCM_LOG_DEV_DBG(ff, "FCoE feature is %ssynced",
					ff->ff_app_info.syncd ? "" :
					"not ");
			FCM_LOG_DEV_DBG(ff, "FCoE operating mode is %s",
					ff->ff_app_info.op_mode ? "on" :
					"off ");
			break;

		default:
			break;
		}
	}
}

/*
 * Handle command response.
 * Response buffer points past command code character in response.
 */
static void fcm_dcbd_cmd_resp(char *resp, cmd_status st)
{
	struct fcm_netif *ff;
	u_int32_t ver;
	u_int32_t cmd;
	u_int32_t feature;
	u_int32_t state;
	char *ep;
	char *cp;
	size_t len;

	resp += CLIF_RSP_OFF;
	len = strlen(resp);
	ver = fcm_get_hex(resp + DCB_VER_OFF, DCB_VER_LEN, &ep);
	if (ep != NULL) {
		FCM_LOG("parse error: resp %s", resp);
		return;
	} else	if (ver != CLIF_RSP_VERSION) {
		FCM_LOG("unexpected version %d resp %s", ver, resp);
		return;
	}
	cmd = fcm_get_hex(resp + DCB_CMD_OFF, DCB_CMD_LEN, &ep);
	if (ep != NULL) {
		FCM_LOG("parse error on resp cmd: resp %s", resp);
		return;
	}
	feature = fcm_get_hex(resp + DCB_FEATURE_OFF, DCB_FEATURE_LEN, &ep);
	if (ep != NULL) {
		FCM_LOG("parse error on resp feature: resp %s", resp);
		return;
	}
	fcm_get_hex(resp + DCB_SUBTYPE_OFF, DCB_SUBTYPE_LEN, &ep);
	if (ep != NULL) {
		FCM_LOG("parse error on resp subtype: resp %s", resp);
		return;
	}
	cp = resp;
	ff = fcm_dcbd_get_port(&cp, DCB_PORTLEN_OFF, DCB_PORTLEN_LEN, len);
	if (ff == NULL) {
		FCM_LOG("port not found. resp %s", resp);
		return;
	}

	/*
	 * check that dcbd response matches the current dcbd state.
	 */
	state = ff->ff_dcbd_state;
	if (((cmd == CMD_GET_CONFIG) &&
	     ((state == FCD_GET_DCB_STATE && feature == FEATURE_DCB) ||
	      (state == FCD_GET_PFC_CONFIG && feature == FEATURE_PFC) ||
	      (state == FCD_GET_APP_CONFIG && feature == FEATURE_APP)))
	    ||
	    ((cmd == CMD_GET_OPER) &&
	     ((state == FCD_GET_PFC_OPER && feature == FEATURE_PFC) ||
	      (state == FCD_GET_APP_OPER && feature == FEATURE_APP)))) {

		/* the response matches the current pending query */
		ff->response_pending = 0;
		if (st != cmd_success) {
			if (st == cmd_not_applicable)
				fcm_dcbd_state_set(ff, FCD_DONE);
			else
				fcm_dcbd_state_set(ff, FCD_ERROR);
			return;
		}
	}

	switch (cmd) {
	case CMD_SET_CONFIG:
		fcm_dcbd_set_config(ff);
		break;

	case CMD_GET_CONFIG:
		fcm_dcbd_get_config(ff, resp);
		break;

	case CMD_GET_OPER:
		fcm_dcbd_get_oper(ff, resp, cp);
		break;

	default:
		FCM_LOG_DEV_DBG(ff, "Unknown cmd 0x%x in response: resp %s",
				cmd, resp);
		break;
	}
}

/*
 * Handle incoming DCB event message.
 * Example message: E5104eth8050001
 */
static void fcm_dcbd_event(char *msg, size_t len)
{
	struct fcm_netif *ff;
	struct fcoe_port *p;
	u_int32_t feature;
	u_int32_t subtype;
	char *cp;
	char *ep;

	if (msg[EV_LEVEL_OFF] != MSG_DCB + '0' || len <= EV_PORT_ID_OFF)
		return;
	if (msg[EV_VERSION_OFF] != CLIF_EV_VERSION + '0') {
		FCM_LOG("Unexpected version in event msg %s", msg);
		return;
	}
	cp = msg;
	ff = fcm_dcbd_get_port(&cp, EV_PORT_LEN_OFF, EV_PORT_LEN_LEN, len);
	if (ff == NULL)
		return;

	feature = fcm_get_hex(cp + EV_FEATURE_OFF, 2, &ep);
	if (ep != NULL) {
		FCM_LOG_DEV_DBG(ff, "Invalid feature code in event msg %s",
				msg);
		return;
	}

	/*
	 * Check if the FCoE ports which use the interface on which the
	 * dcbd event arrived are configured to require dcb.
	 */

	p = fcm_find_fcoe_port(ff->ifname, FCP_REAL_IFNAME);
	while (p) {
		if (p->dcb_required && p->last_msg_type != RTM_DELLINK &&
		    p->fcoe_enable)
			break;
		p = fcm_find_next_fcoe_port(p, ff->ifname);
	}

	/*
	 * dcb is not required or link was removed, ignore dcbd event
	 */
	if (!p)
		return;

	if (ff->ff_operstate != IF_OPER_UP)
		return;

	switch (feature) {
	case FEATURE_PG:     /* 'E5204eth2020001' */
		FCM_LOG_DEV_DBG(ff, "<Got PG Event>\n");
		break;
	case FEATURE_PFC:    /* 'E5204eth2030011' */
		FCM_LOG_DEV_DBG(ff, "<Got PFC Event>\n");
		fcm_dcbd_state_set(ff, FCD_GET_DCB_STATE);
		break;
	case FEATURE_APP:    /* 'E5204eth2050011' */
		FCM_LOG_DEV_DBG(ff, "<Got APP Event>\n");
		subtype = fcm_get_hex(cp + EV_SUBTYPE_OFF, 2, &ep);
		if (subtype != APP_FCOE_STYPE) {
			FCM_LOG_DEV_DBG(ff, "Unknown application subtype "
					"in msg %s", msg);
			break;
		}
		fcm_dcbd_state_set(ff, FCD_GET_DCB_STATE);
		break;
	default:
		FCM_LOG_DEV_DBG(ff, "Unknown feature 0x%x in msg %s",
				feature, msg);
		break;
	}

	if (fcoe_config.debug) {
		if (cp[EV_OP_MODE_CHG_OFF] == '1')
			FCM_LOG_DEV_DBG(ff,
					"Operational mode changed");
		if (cp[EV_OP_CFG_CHG_OFF] == '1')
			FCM_LOG_DEV_DBG(ff,
					"Operational config changed");
	}
}

/*
 * The status is interpreted by the client as an 'enum fcoe_status'.
 */
static void fcm_cli_reply(struct sock_info *r, enum fcoe_status status)
{
	char rbuf[MAX_MSGBUF];
	snprintf(rbuf, MSG_RBUF, "%d", status);
	sendto(r->sock, rbuf, MSG_RBUF, 0, (struct sockaddr *)&(r->from),
	       r->fromlen);
}

static enum fcoe_status fcm_fcoe_if_action(char *path, char *ifname)
{
	FILE *fp = NULL;
	enum fcoe_status ret = EFAIL;

	fp = fopen(path, "w");
	if (!fp) {
		FCM_LOG_ERR(errno, "%s: Failed to open path %s\n",
			    progname, path);
		goto err_out;
	}

	if (EOF == fputs(ifname, fp)) {
		FCM_LOG_ERR(errno, "%s: Failed to write %s to path %s.\n",
			    progname, ifname, path);
		goto out;
	}

	ret = SUCCESS;
out:
	fclose(fp);
err_out:
	return ret;
}

static void fcm_send_fip_request(const struct fcoe_port *p)
{
	int dest;

	if (p->mode == FCOE_MODE_VN2VN)
		dest = FIP_ALL_VN2VN;
	else
		dest = FIP_ALL_FCF;
	fip_send_vlan_request(p->fip_socket, p->ifindex, p->mac, dest);
}

void fcm_vlan_disc_timeout(void *arg)
{
	struct fcoe_port *p = arg;
	FCM_LOG_DBG("%s: VLAN discovery TIMEOUT [%d]",
		    p->ifname, p->vlan_disc_count);
	p->vlan_disc_count++;
	fcm_send_fip_request(p);
	sa_timer_set(&p->vlan_disc_timer, FCM_VLAN_DISC_TIMEOUT);
}

static int fcm_start_vlan_disc(struct fcoe_port *p)
{
	int s;
	if (p->fip_socket < 0) {
		s = fcm_vlan_disc_socket(p);
		if (s < 0)
			return s;
		p->fip_socket = s;
	}
	p->vlan_disc_count = 1;
	fcm_send_fip_request(p);
	sa_timer_set(&p->vlan_disc_timer, FCM_VLAN_DISC_TIMEOUT);
	return 0;
}

/*
 *
 * Input:  action = 1      Destroy the FCoE interface
 *         action = 2      Create the FCoE interface
 *         action = 3      Reset the interface
 */
static void fcm_fcoe_action(struct fcoe_port *p)
{
	struct fcoe_port *vp;
	char path[MAX_PATH_LEN];
	enum fcoe_status rc = SUCCESS;

	switch (p->action) {
	case FCP_CREATE_IF:
		FCM_LOG_DBG("OP: CREATE %s\n", p->ifname);
		rc = libfcoe_control->create(p);
		if (rc) {
			FCM_LOG_DBG("Failed to create FCoE interface "
				    "for %s, rc is %d\n", p->ifname, rc);
			break;
		}

		FCM_LOG_DBG("OP: created fchost:%s for %s\n",
			    p->fchost, p->ifname);

		break;
	case FCP_DESTROY_IF:
		FCM_LOG_DBG("OP: DESTROY %s\n", p->ifname);
		if (p->auto_vlan) {
			/* destroy all the VLANs */
			vp = fcm_find_fcoe_port(p->ifname, FCP_REAL_IFNAME);
			while (vp) {
				if (vp->auto_created) {
					vp->ready = 0;
					fcp_set_next_action(vp, FCP_DESTROY_IF);
				}
				vp = fcm_find_next_fcoe_port(vp, p->ifname);
			}
			rc = SUCCESS;
			break;
		}
		rc = libfcoe_control->destroy(p);
		p->fchost[0] = '\0';
		break;
	case FCP_ENABLE_IF:
		FCM_LOG_DBG("OP: ENABLE %s\n", p->ifname);
		rc = libfcoe_control->enable(p);
		break;
	case FCP_DISABLE_IF:
		FCM_LOG_DBG("OP: DISABLE %s\n", p->ifname);
		if (p->auto_vlan) {
			/* disable all the VLANs */
			vp = fcm_find_fcoe_port(p->ifname, FCP_REAL_IFNAME);
			while (vp) {
				if (vp->auto_created) {
					vp->ready = 0;
					fcp_set_next_action(vp, FCP_DISABLE_IF);
				}
				vp = fcm_find_next_fcoe_port(vp, p->ifname);
			}
			break;
		}
		rc = libfcoe_control->disable(p);
		break;
	case FCP_RESET_IF:
		FCM_LOG_DBG("OP: RESET %s\n", p->ifname);

		if (strlen(p->fchost) <= 0)  {
			fcm_cli_reply(p->sock_reply, ENOFCHOST);
			return;
		}

		sprintf(path, "%s/%s/issue_lip", SYSFS_FCHOST, p->fchost);
		FCM_LOG_DBG("OP: RESET %s\n", path);
		rc = fcm_fcoe_if_action(path, "1");
		break;
	case FCP_SCAN_IF:
		FCM_LOG_DBG("OP: SCAN %s\n", p->ifname);
		if (strlen(p->fchost) <= 0)  {
			fcm_cli_reply(p->sock_reply, ENOFCHOST);
			return;
		}

		sprintf(path, "%s/%s/device/scsi_host/%s/scan",
			SYSFS_FCHOST, p->fchost, p->fchost);
		FCM_LOG_DBG("OP: SCAN %s\n", path);
		rc = fcm_fcoe_if_action(path, "- - -");
		break;
	case FCP_VLAN_DISC:
		FCM_LOG_DBG("OP: VLAN DISC %s\n", p->ifname);
		rc = fcm_start_vlan_disc(p);
		break;
	default:
		return;
		break;
	}

	if (p->sock_reply) {
		fcm_cli_reply(p->sock_reply, rc);
		free(p->sock_reply);
		p->sock_reply = NULL;
	}

	p->last_action = p->action;
}

/*
 * Called for all ports.  For FCoE ports and candidates,
 * get IEEE DCBX information and set the next action.
 */
static void fcm_netif_ieee_advance(struct fcm_netif *ff)
{
	enum fcp_action action;

	ASSERT(ff);
	ASSERT(fcm_clif);

	if (fcm_clif->cl_busy)
		return;

	if (ff->ieee_resp_pending)
		return;

	switch (ff->ieee_state) {
	case IEEE_INIT:
		break;
	case IEEE_GET_STATE:
		ieee_get_req(ff);
		break;
	case IEEE_DONE:
		action = validate_ieee_info(ff);
		if (action == FCP_ACTIVATE_IF) {
			fcp_action_set(ff->ifname, action);
			ieee_state_set(ff, IEEE_ACTIVE);
		}
		break;
	case IEEE_ACTIVE:
		/* TBD enable and disable if needed in IEEE mode */
		break;
	default:
		break;
	}
}

/*
 * Called for all ports.  For FCoE ports and candidates,
 * get information and send to dcbd.
 */
static void fcm_netif_advance(struct fcm_netif *ff)
{
	char buf[80], params[30];

	ASSERT(ff);
	ASSERT(fcm_clif);

	if (fcm_clif->cl_busy)
		return;

	if (ff->response_pending)
		return;

	if (sa_timer_active(&ff->dcbd_retry_timer))
		return;

	switch (ff->ff_dcbd_state) {
	case FCD_INIT:
	case FCD_ERROR:
		break;
	case FCD_GET_DCB_STATE:
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_GET_CONFIG, FEATURE_DCB, 0,
			 (u_int) strlen(ff->ifname), ff->ifname);
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_SEND_CONF:
		snprintf(params, sizeof(params), "%x1%x02",
			 ff->ff_app_info.enable,
			 ff->ff_app_info.willing);
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_SET_CONFIG, FEATURE_APP, APP_FCOE_STYPE,
			 (u_int) strlen(ff->ifname), ff->ifname, params);
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_GET_PFC_CONFIG:
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_GET_CONFIG, FEATURE_PFC, 0,
			 (u_int) strlen(ff->ifname), ff->ifname, "");
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_GET_APP_CONFIG:
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_GET_CONFIG, FEATURE_APP, APP_FCOE_STYPE,
			 (u_int) strlen(ff->ifname), ff->ifname, "");
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_GET_PFC_OPER:
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_GET_OPER, FEATURE_PFC, 0,
			 (u_int) strlen(ff->ifname), ff->ifname, "");
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_GET_APP_OPER:
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_GET_OPER, FEATURE_APP, APP_FCOE_STYPE,
			 (u_int) strlen(ff->ifname), ff->ifname, "");
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_GET_PEER:
		snprintf(buf, sizeof(buf), "%c%x%2.2x%2.2x%2.2x%2.2x%s%s",
			 DCB_CMD, CLIF_RSP_VERSION,
			 CMD_GET_PEER, FEATURE_APP, APP_FCOE_STYPE,
			 (u_int) strlen(ff->ifname), ff->ifname, "");
		ff->response_pending = fcm_dcbd_request(buf);
		break;
	case FCD_DONE:
		switch (validate_dcbd_info(ff)) {
		case FCP_DESTROY_IF:
			fcp_action_set(ff->ifname, FCP_DESTROY_IF);
			fcm_dcbd_state_set(ff, FCD_INIT);
			break;
		case FCP_ENABLE_IF:
			fcp_action_set(ff->ifname, FCP_ENABLE_IF);
			fcm_dcbd_state_set(ff, FCD_INIT);
			break;
		case FCP_DISABLE_IF:
			fcp_action_set(ff->ifname, FCP_DISABLE_IF);
			fcm_dcbd_state_set(ff, FCD_INIT);
			break;
		case FCP_ACTIVATE_IF:
			fcp_action_set(ff->ifname, FCP_ACTIVATE_IF);
			fcm_dcbd_state_set(ff, FCD_INIT);
			break;
		case FCP_ERROR:
			fcp_action_set(ff->ifname, FCP_DISABLE_IF);
			if (ff->dcbd_retry_cnt < DCBD_MAX_REQ_RETRIES)
				fcm_dcbd_state_set(ff, FCD_ERROR);
			else
				fcm_dcbd_state_set(ff, FCD_INIT);
			break;
		case FCP_WAIT:
		default:
			break;
		}

		break;
	default:
		break;
	}
}

/*
 * Run through these steps at the end of each select loop.
 * 1.  Process list of network interfaces
 *     - issue next dcbd query action
 *     - if query sequence is complete - update FCoE port objects
 *       as necessary with a CREATE or DESTROY next action.
 * 2.  Process FCoE port list - handle next actions, update states, clean up
 */
static void fcm_handle_changes(void)
{
	struct fcm_netif *ff;
	struct fcoe_port *p;

	/*
	 * Perform pending actions (dcbd queries) on network interfaces.
	 */
	TAILQ_FOREACH(ff, &fcm_netif_head, ff_list) {
		fcm_netif_advance(ff);
		fcm_netif_ieee_advance(ff);
	}

	/*
	 * Perform actions on FCoE ports
	 */
	p = fcoe_config.port;
	while (p) {
		ff = fcm_netif_lookup(p->real_ifname);
		if (!ff) {
			if (p->sock_reply) {
				fcm_cli_reply(p->sock_reply, ENOETHDEV);
				free(p->sock_reply);
				p->sock_reply = NULL;
				p->action = FCP_WAIT;
			}
			goto next_port;
		}

		fcm_fcoe_action(p);

		fcp_set_next_action(p, FCP_WAIT);
next_port:
		p = p->next;
	}
}

static void fcm_usage(void)
{
	printf("Usage: %s\n"
	       "\t [-f|--foreground]\n"
	       "\t [-d|--debug]\n"
	       "\t [-l|--legacy]\n"
	       "\t [-s|--syslog]\n"
	       "\t [-v|--version]\n"
	       "\t [-h|--help]\n\n", progname);
	exit(1);
}

static void fcm_dump(void)
{
	struct fcoe_port *curr, *next;
	struct fcm_netif *ff, *head;

	FCM_LOG("*** !!! fcoemon dump begin !!! ***\n");

	FCM_LOG("*** Listing of fcoe_port instances ***\n");
	for (curr = fcoe_config.port; curr; curr = next) {
		FCM_LOG("** ifname: %s\n", curr->ifname);
		FCM_LOG("ifindex: %d\n", curr->ifindex);
		FCM_LOG("real_ifname: %s\n", curr->real_ifname);
		FCM_LOG("fcoe_enable: %d\n", curr->fcoe_enable);
		FCM_LOG("dcb_required: %d\n", curr->dcb_required);
		FCM_LOG("auto_vlan: %d\n", curr->auto_vlan);
		FCM_LOG("auto_created: %d\n", curr->auto_created);
		FCM_LOG("ready: %d\n", curr->ready);
		FCM_LOG("action: %d\n", curr->action);
		FCM_LOG("last_action: %d\n", curr->last_action);
		FCM_LOG("last_msg_type: %d\n", curr->last_msg_type);
		FCM_LOG("mac: %s\n", curr->mac); //TODO
		FCM_LOG("vlan_disc_count: %d\n", curr->vlan_disc_count);
		FCM_LOG("fip_socket: %d\n", curr->fip_socket); //TODO
		FCM_LOG("fchost: %s\n", curr->fchost);
		FCM_LOG("ctlr: %s\n", curr->ctlr);
		FCM_LOG("last_fc_event_num: %d\n", curr->last_fc_event_num);

		next = curr->next;
	}

	FCM_LOG("*** Listing of fcm_netif instances ***\n");
	for (head = TAILQ_FIRST(&fcm_netif_head); head; head = ff) {
		FCM_LOG("** ifname: %s", head->ifname);
		FCM_LOG("ff_enabled: %d\n", head->ff_enabled);
		FCM_LOG("ff_dcb_state: %d\n", head->ff_dcb_state);
		FCM_LOG("dcbx_cap: %d\n", head->dcbx_cap);
		FCM_LOG("ieee_state: %d\n", head->ieee_state);
		FCM_LOG("ieee_resp_pending: %d\n", head->ieee_resp_pending);
		FCM_LOG("ieee_pfc_info: %d\n", head->ieee_pfc_info);
		FCM_LOG("ieee_app_info: %d\n", head->ieee_app_info);
		FCM_LOG("ff_pfc_info: %d\n", head->ff_pfc_info.op_mode);
		FCM_LOG("ff_app_info: %d\n", head->ff_app_info.op_mode);
		FCM_LOG("ff_operstate: %d\n", head->ff_operstate);
		FCM_LOG("ff_dcb_state: %d\n", head->ff_dcb_state);
		FCM_LOG("response_pending: %d\n", head->response_pending);
		FCM_LOG("dcbd_retry_cnt: %d\n", head->dcbd_retry_cnt);

		ff = TAILQ_NEXT(head, ff_list);
	}

	FCM_LOG("*** !!! fcoemon dump end !!! ***\n");
}

static void fcm_sig(int sig)
{
	switch (sig) {
	case SIGUSR1:
		fcm_dump();
		break;
	default:
		sa_select_exit(sig);
		break;
	}
}

/*
 * TODO: This routine does too much. It executes a 'cmd'
 * and allocates a fcoe_port if one doesn't exist. The
 * function name implies that it only does the latter.
 */
static struct fcoe_port *
fcm_port_create(char *ifname, enum clif_flags flags, int cmd)
{
	struct fcoe_port *p;
	struct fcoe_port *curr;
	struct fcm_netif *ff;

	p = fcm_find_fcoe_port(ifname, FCP_CFG_IFNAME);
	if (p) {
		p->ready = 1;
		if (!p->fcoe_enable) {
			p->fcoe_enable = 1;
			fcp_set_next_action(p, cmd);
			if (p->dcb_required) {
				ff = fcm_netif_lookup(p->real_ifname);
				if (!ff)
					return p;
				fcm_dcbd_state_set(ff, FCD_GET_DCB_STATE);
				if (ff->ff_dcbd_state == FCD_GET_DCB_STATE)
					fcp_set_next_action(p, FCP_WAIT);
			}
		} else {
			p->fcoe_enable = 1;
			fcp_set_next_action(p, cmd);
		}
		return p;
	}

	p = alloc_fcoe_port(ifname);
	if (!p) {
		FCM_LOG_ERR(errno, "fail to allocate fcoe_port %s", ifname);
		return NULL;
	}

	fcm_vlan_dev_real_dev(ifname, p->real_ifname);
	if (!strlen(p->real_ifname))
		snprintf(p->real_ifname, sizeof(p->real_ifname), "%s", ifname);
	p->fcoe_enable = 1;
	p->dcb_required = 0;
	p->mode = flags & CLIF_FLAGS_MODE_MASK;
	fcp_set_next_action(p, cmd);
	p->next = NULL;

	sigprocmask(SIG_BLOCK, &block_sigset, NULL);

	if (!fcoe_config.port)
		fcoe_config.port = p;
	else {
		curr = fcoe_config.port;
		while (curr->next)
			curr = curr->next;
		curr->next = p;
	}

	sigprocmask(SIG_UNBLOCK, &block_sigset, NULL);

	/* check and add the real_ifname to the network interface list */
	ff = fcm_netif_lookup_create(p->real_ifname);
	if (!ff) {
		FCM_LOG_ERR(errno, "fail to allocate fcm_netif %s", ifname);
		return NULL;
	}
	return p;
}

static enum fcoe_status fcm_cli_create(char *ifname, enum clif_flags flags,
				       struct sock_info **r)
{
	struct fcoe_port *p, *vp;
	enum fcoe_status rc = EFAIL;
	char fchost[FCHOSTBUFLEN];

	/* existing fchost already? e.g., from fipvlan -cs */
	if (!fcoe_find_fchost(ifname, fchost, FCHOSTBUFLEN)) {
		FCM_LOG_DBG("Existing fchost %s found for %s\n",
			    fchost, ifname);
		rc = EFCOECONN;
		goto out;
	}

	p = fcm_find_fcoe_port(ifname, FCP_CFG_IFNAME);
	if (p && p->fcoe_enable) {
		/* no action needed */
		rc = EFCOECONN;
		goto out;
	}
	/* re-enable previous VLANs */
	if (p && p->auto_vlan) {
		vp = fcm_find_fcoe_port(p->ifname, FCP_REAL_IFNAME);
		while (vp) {
			if (vp->auto_created)
				vp->fcoe_enable = 1;
			vp = fcm_find_next_fcoe_port(vp, p->ifname);
		}
	}

	/*
	 * This looks odd, and could use some improvement. We may
	 * or may not have found a valid port. fcm_port_create
	 * will execute the 'cmd' even if it doesn't allocate a
	 * new port. fcm_port_create should probably be split
	 * into two routines, one that allocs a new port and one
	 * that executes the command.
	 */
	p = fcm_port_create(ifname, flags, FCP_CREATE_IF);
	if (!p)
		goto out;

	rc = SUCCESS;
	p->sock_reply = *r;

out:
	return rc;
}

static enum fcoe_status fcm_cli_destroy(char *ifname,
					struct sock_info **r)
{
	struct fcoe_port *p;

	p = fcm_find_fcoe_port(ifname, FCP_CFG_IFNAME);
	if (p) {
		if (p->fcoe_enable) {
			p->fcoe_enable = 0;

			if (p->last_action == FCP_DESTROY_IF)
				return ENOACTION;

			fcp_set_next_action(p, FCP_DESTROY_IF);
			p->sock_reply = *r;
			return SUCCESS;
		} else {
			/* no action needed */
			return ENOACTION;
		}
	}

	FCM_LOG_ERR(errno, "%s is not in port list.\n", ifname);
	return EFAIL;
}

static enum fcoe_status fcm_cli_action(char *ifname, int cmd,
				       struct sock_info **r)
{
	struct fcoe_port *p;

	p = fcm_find_fcoe_port(ifname, FCP_CFG_IFNAME);
	if (p) {
		fcp_set_next_action(p, cmd);
		p->sock_reply = *r;
		return SUCCESS;
	}

	FCM_LOG_ERR(errno, "%s is not in port list.\n", ifname);
	return EFAIL;
}

static struct sock_info *fcm_alloc_reply(struct sockaddr_un *f,
					 socklen_t flen, int s)
{
	static struct sock_info *r;

	if (flen > sizeof(*f))
		return NULL;

	r = (struct sock_info *)malloc(sizeof(struct sock_info));
	if (!r) {
		FCM_LOG_ERR(errno, "Failed in alloc reply sock info.\n");
		return NULL;
	}
	r->sock = s;
	memcpy(&r->from, f, flen);
	r->fromlen = flen;
	return r;
}

/*
 * receive function registered in sa_select_loop
 */
static void fcm_srv_receive(void *arg)
{
	struct fcm_srv_info *srv_info = arg;
	struct clif_data *data;
	struct sockaddr_un from;
	socklen_t fromlen = sizeof(struct sockaddr_un);
	struct sock_info *reply = NULL;
	char buf[MAX_MSGBUF], rbuf[MAX_MSGBUF];
	char ifname[sizeof(data->ifname) + 1];
	enum fcoe_status rc = EFAIL;
	int res, cmd, snum;
	size_t size;

	snum = srv_info->srv_sock;
	res = recvfrom(snum, buf, sizeof(buf) - 1,
		       MSG_DONTWAIT, (struct sockaddr *)&from, &fromlen);
	if (res < 0) {
		FCM_LOG_ERR(errno, "Failed to receive data from socket %d",
			    snum);
		return;
	}

	data = (struct clif_data *)buf;
	size = res;
	if (size < sizeof(*data)) {
		if (size < sizeof(*data) - sizeof(data->flags)) {
			FCM_LOG_ERR(EMSGSIZE,
				    "Message too short from socket %d", snum);
			rc = EBADCLIFMSG;
			goto err;
		}
		data->flags = 0;
	} else if (size > sizeof(*data)) {
		FCM_LOG_ERR(EMSGSIZE, "Message too long from socket %d", snum);
		rc = EBADCLIFMSG;
		goto err;
	}

	cmd = data->cmd;
	strncpy(ifname, data->ifname, sizeof(ifname));
	ifname[sizeof(data->ifname)] = 0;

	if (cmd != CLIF_PID_CMD) {
		rc = fcoe_validate_interface(ifname);
		if (rc)
			goto err;
	}
	reply = fcm_alloc_reply(&from, fromlen, snum);
	if (!reply)
		goto err_out;

	switch (cmd) {
	case CLIF_CREATE_CMD:
		FCM_LOG_DBG("Received command to create %s\n", ifname);
		rc = fcm_cli_create(ifname, data->flags, &reply);
		if (rc)
			goto err_out;
		break;
	case CLIF_DESTROY_CMD:
		FCM_LOG_DBG("Received command to destroy %s\n", ifname);
		rc = fcm_cli_destroy(ifname, &reply);
		if (rc)
			goto err_out;
		break;
	case CLIF_RESET_CMD:
		FCM_LOG_DBG("Received command to reset %s\n", ifname);
		rc = fcm_cli_action(ifname, FCP_RESET_IF, &reply);
		if (rc)
			goto err_out;
		break;
	case CLIF_SCAN_CMD:
		FCM_LOG_DBG("Received command to scan %s\n", ifname);
		rc = fcm_cli_action(ifname, FCP_SCAN_IF, &reply);
		if (rc)
			goto err_out;
		break;
	case CLIF_PID_CMD:
		FCM_LOG_DBG("FCMON PID\n");
		snprintf(rbuf, MAX_MSGBUF, "%lu", (long unsigned int)getpid());
		sendto(snum, rbuf, strlen(rbuf), 0, (struct sockaddr *)&from,
		       fromlen);
		break;
	default:
		FCM_LOG_DBG("Received invalid command %d for %s\n",
			    cmd, ifname);
		goto err_out;
	}

	return;

err_out:
	free(reply);
err:
	snprintf(rbuf, MSG_RBUF, "%d", rc);
	sendto(snum, rbuf, MSG_RBUF, 0, (struct sockaddr *)&from, fromlen);
}

static int fcm_systemd_socket(void)
{
	char *env, *ptr;
	unsigned int p, l;

	env = getenv("LISTEN_PID");
	if (!env)
		return -1;

	p = strtoul(env, &ptr, 10);
	if (ptr && ptr == env) {
		FCM_LOG_DBG("Invalid value '%s' for LISTEN_PID\n", env);
		return -1;
	}
	if ((pid_t)p != getpid()) {
		FCM_LOG_DBG("Invalid PID '%d' from LISTEN_PID\n", p);
		return -1;
	}
	env = getenv("LISTEN_FDS");
	if (!env) {
		FCM_LOG_DBG("LISTEN_FDS is not set\n");
		return -1;
	}
	l = strtoul(env, &ptr, 10);
	if (ptr && ptr == env) {
		FCM_LOG_DBG("Invalid value '%s' for LISTEN_FDS\n", env);
		return -1;
	}
	if (l != 1) {
		FCM_LOG_DBG("LISTEN_FDS specified %d fds\n", l);
		return -1;
	}
	/* systemd returns fds with an offset of '3' */
	return 3;
}

static int fcm_srv_create(struct fcm_srv_info *srv_info)
{
	socklen_t addrlen;
	struct sockaddr_un addr;
	int rc = 0;

	srv_info->srv_sock = fcm_systemd_socket();
	if (srv_info->srv_sock > 0) {
		FCM_LOG_DBG("Using systemd socket\n");
		goto out_done;
	}

	srv_info->srv_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
	if (srv_info->srv_sock < 0) {
		FCM_LOG_ERR(errno, "Failed to create socket\n");
		rc = errno;
		goto err;
	}

	memset(&addr, 0, sizeof(addr));
	addr.sun_family = AF_LOCAL;
	addr.sun_path[0] = '\0';
	snprintf(&addr.sun_path[1], sizeof(addr.sun_path) -1,
		 "%s", CLIF_IFNAME);
	addrlen = sizeof(sa_family_t) + strlen(addr.sun_path + 1) + 1;
	if (bind(srv_info->srv_sock, (struct sockaddr *)&addr, addrlen) < 0) {
		FCM_LOG_ERR(errno, "Failed to bind socket\n");
		rc = errno;
		goto err_close;
	}
out_done:
	sa_select_add_fd(srv_info->srv_sock, fcm_srv_receive,
			 NULL, NULL, srv_info);

	FCM_LOG_DBG("Successfully created socket, socket file and binding\n");

	return rc;

err_close:
	close(srv_info->srv_sock);

err:
	return rc;
}

static void fcm_srv_destroy(struct fcm_srv_info *srv_info)
{
	FCM_LOG_DBG("Shutdown fcmon server");
	close(srv_info->srv_sock);
}

int main(int argc, char **argv)
{
	struct fcm_srv_info srv_info;
	struct sigaction sig;
	int fcm_fg = 0;
	int rc;
	int c;

	memset(&fcoe_config, 0, sizeof(fcoe_config));

	strncpy(progname, basename(argv[0]), sizeof(progname));
	sa_log_prefix = progname;
	sa_log_flags = 0;
	openlog(sa_log_prefix, LOG_CONS, LOG_DAEMON);

	while ((c = getopt_long(argc, argv, "fdhlsv",
				fcm_options, NULL)) != -1) {
		switch (c) {
		case 'f':
			fcm_fg = 1;
			break;
		case 'd':
			fcoe_config.debug = 1;
			enable_debug_log(1);
			break;
		case 'l':
			force_legacy = true;
			break;
		case 's':
			fcoe_config.use_syslog = 1;
			enable_syslog(1);
			break;
		case 'v':
			printf("%s\n", FCOE_UTILS_VERSION);
			return 0;
		case 'h':
		default:
			fcm_usage();
		break;
		}
	}
	if (argc != optind)
		fcm_usage();

	umask(0);

	/*
	 * Set up for signals.
	 */
	memset(&sig, 0, sizeof(sig));
	sig.sa_handler = fcm_sig;

	/*
	 * SIGUSR1 will walk the fcoe_port and fcm_netif lists,
	 * therefore we need to disable this interrupt when we
	 * do any list manipulations. The 'block_sigset' is the
	 * set of signals that will be blocked before list
	 * manipulations on either of the aforementioned lists.
	 */
	sigemptyset(&block_sigset);
	sigaddset(&block_sigset, SIGUSR1);

	rc = sigaction(SIGINT, &sig, NULL);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "Failed to register handler for SIGINT");
		exit(1);
	}
	rc = sigaction(SIGTERM, &sig, NULL);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "Failed to register handler for SIGTERM");
		exit(1);
	}
	rc = sigaction(SIGHUP, &sig, NULL);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "Failed to register handler for SIGHUP");
		exit(1);
	}
	rc = sigaction(SIGUSR1, &sig, NULL);
	if (rc < 0) {
		FCM_LOG_ERR(errno, "Failed to register handler for SIGUSR1");
		exit(1);
	}

	/* check fcoe module */
	if (fcoe_checkdir(SYSFS_FCOE)) {
		FCM_LOG_ERR(errno, "make sure FCoE driver module is loaded!");
		exit(1);
	}

	fcm_fcoe_init();
	rc = fcm_fc_events_init();
	if (rc != 0)
		exit(1);

	rc = fcm_link_init();	/* NETLINK_ROUTE protocol */
	if (rc != 0)
		goto err_cleanup;

	if (fcoe_config.dcb_init)
		fcm_dcbd_init();

	rc = fcm_srv_create(&srv_info);
	if (rc != 0)
		goto err_cleanup;

	if (!fcm_fg && daemon(0, !fcoe_config.use_syslog)) {
		FCM_LOG("Starting daemon failed");
		goto err_cleanup;
	}

	sa_select_set_callback(fcm_handle_changes);

	rc = sa_select_loop();
	if (rc < 0) {
		FCM_LOG_ERR(rc, "select error\n");
		goto err_cleanup;
	}
	fcm_dcbd_shutdown();
	fcm_srv_destroy(&srv_info);
	if (rc == SIGHUP)
		fcm_cleanup();
	return 0;

err_cleanup:
	fcm_cleanup();
	exit(1);
}

/*******************************************************
 *         The following are debug routines            *
 *******************************************************/
static void add_msg_to_buf(char *buf, size_t maxlen, char *msg, char *prefix)
{
	size_t len = strlen(buf);

	if (len + strlen(msg) + strlen(prefix) < maxlen)
		sprintf(buf+len, "%s%s", prefix, msg);
}

static void print_errors(int errors)
{
	char msg[256];
	int cnt = 0;

	memset(msg, 0, sizeof(msg));
	sprintf(msg, "0x%02x - ", errors);

	if (errors & 0x01)
		add_msg_to_buf(msg, sizeof(msg), "mismatch with peer",
			       (cnt++) ? ", " : "");

	if (errors & 0x02)
		add_msg_to_buf(msg, sizeof(msg), "local configuration error",
			       (cnt++) ? ", " : "");

	if (errors & 0x04)
		add_msg_to_buf(msg, sizeof(msg), "multiple TLV's received",
			       (cnt++) ? ", " : "");

	if (errors & 0x08)
		add_msg_to_buf(msg, sizeof(msg), "peer error",
			       (cnt++) ? ", " : "");

	if (errors & 0x10)
		add_msg_to_buf(msg, sizeof(msg), "multiple LLDP neighbors",
			       (cnt++) ? ", " : "");

	if (errors & 0x20)
		add_msg_to_buf(msg, sizeof(msg), "peer feature not present",
			       (cnt++) ? ", " : "");

	if (!errors)
		add_msg_to_buf(msg, sizeof(msg), "none", "");

	FCM_LOG("%s\n", msg);
}