summaryrefslogtreecommitdiff
path: root/iscsiuio/src/unix/nic_utils.c
blob: ec4535de0e23681cb0e616fbad2e5d6804b5cc59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
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
/*
 * Copyright (c) 2009-2011, Broadcom Corporation
 * Copyright (c) 2014, QLogic Corporation
 *
 * Written by:  Benjamin Li  (benli@broadcom.com)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed by Adam Dunkels.
 * 4. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * nic_util.c - shared NIC utility functions
 *
 */
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>

#include "logger.h"
#include "nic.h"
#include "nic_id.h"
#include "nic_vlan.h"
#include "nic_utils.h"
#include "options.h"

#define PFX "nic_utils "

/******************************************************************************
 *  String constants
 *****************************************************************************/
static const char nic_uio_sysfs_name_tempate[] = "/sys/class/uio/uio%i/name";
static const char cnic_sysfs_uio_event_template[] =
	"/sys/class/uio/uio%d/event";
static const char base_uio_sysfs_name[] = "/sys/class/uio/";
static const char uio_name[] = "uio";

static const char uio_base_dir[] = "/dev/uio";
static const char uio_udev_path_template[] = "/dev/uio%hd";
static const char uio_uevent_path_template[] = "/sys/class/uio/uio%d/uevent";

static const char base_iscsi_host_name[] = "/sys/class/iscsi_host/";
static const char host_template[] = "host%d";
static const char iscsi_host_path_netdev_template[] =
	"/sys/class/iscsi_host/host%d/netdev";
static const char cnic_uio_sysfs_resc_template[] =
	"/sys/class/uio/uio%i/device/resource%i";
static const char iscsi_transport_handle_template[] =
	"/sys/class/iscsi_transport/%s/handle";
static const char host_pfx[] = "host";

/**
 *  manually_trigger_uio_event() - If the uio file node doesn't exist then
 *                                 try to retrigger udev to create the file
 *                                 node by touch the uevent file in sysfs
 *  @param nic - the nic to trigger on
 *  @param uio_minor - UIO the minor number to use
 *  @return 0 on success
 */
int manually_trigger_uio_event(nic_t *nic, int uio_minor)
{
	int fd;
	char uio_uevent_path[sizeof(uio_uevent_path_template) + 10];
	char enable_str[] = "online";
	int rc;
	size_t bytes_wrote;

	rc = sprintf(uio_uevent_path, uio_uevent_path_template, uio_minor);
	if (rc < 0) {
		LOG_ERR(PFX "%s: Could not build uio uevent path",
			nic->log_name);
		return -EIO;
	}

	LOG_DEBUG(PFX "%s: triggering UIO uevent path: %s",
		  nic->log_name, uio_uevent_path);

	fd = open(uio_uevent_path, O_WRONLY);
	if (fd == -1) {
		LOG_ERR(PFX "%s: Could not open uio uevent path: %s [%s]",
			nic->log_name, uio_uevent_path, strerror(errno));
		return -EIO;
	}

	bytes_wrote = write(fd, enable_str, sizeof(enable_str));
	if (bytes_wrote != sizeof(enable_str)) {
		LOG_ERR(PFX "%s: Could write to uio uevent path: %s [%s]",
			nic->log_name, uio_uevent_path, strerror(errno));
		rc = -EIO;
	} else
		rc = 0;

	close(fd);
	return rc;
}

static int wait_for_file_node_timed(nic_t *nic, char *filepath, int seconds)
{
	struct timeval start_time;
	struct timeval wait_time;
	struct timeval total_time;
	struct timespec sleep_req, sleep_rem;

	sleep_req.tv_sec = 0;
	sleep_req.tv_nsec = 250000000;

	wait_time.tv_sec = seconds;
	wait_time.tv_usec = 0;

	if (gettimeofday(&start_time, NULL)) {
		LOG_ERR(PFX "%s: Couldn't gettimeofday() during watch file: %s"
			"[%s]", nic->log_name, filepath, strerror(errno));
		return -EIO;
	}

	timeradd(&start_time, &wait_time, &total_time);

	while (1) {
		struct timeval current_time;
		struct stat file_stat;

		/*  Check if the file node exists */
		if (stat(filepath, &file_stat) == 0)
			return 0;

		if (gettimeofday(&current_time, NULL)) {
			LOG_ERR(PFX "%s: Couldn't get current time for "
				"watching file: %s [%s]",
				nic->log_name, filepath, strerror(errno));
			return -EIO;
		}

		/*  Timeout has excceded return -ETIME */
		if (timercmp(&total_time, &current_time, <)) {
			LOG_ERR(PFX "%s: timeout waiting %d secs for file: %s",
				nic->log_name, seconds, filepath);
			return -ETIME;
		}

		nanosleep(&sleep_req, &sleep_rem);
	}
}

/******************************************************************************
 *  Autodiscovery of iscsi_hosts
 *****************************************************************************/
static int filter_host_name(const struct dirent *entry)
{
	if ((memcmp(entry->d_name, "host", 4) == 0))
		return 1;
	else
		return 0;
}

int nic_discover_iscsi_hosts()
{
	struct dirent **files;
	int count;
	int i;
	int rc;

	count = scandir(base_iscsi_host_name, &files, filter_host_name,
			alphasort);

	switch (count) {
	case 0:
		/*  Currently there are no iSCSI hosts */
		rc = 0;
		break;

	case -1:
		LOG_WARN(PFX "Error when scanning path: %s[%s]",
			 base_iscsi_host_name, strerror(errno));
		rc = -EINVAL;
		break;

	default:
		/*  There are iSCSI hosts */
		pthread_mutex_lock(&nic_list_mutex);
		for (i = 0; i < count; i++) {
			int host_no;
			char *raw = NULL;
			uint32_t raw_size = 0;
			char temp_path[sizeof(iscsi_host_path_netdev_template) +
				       8];
			rc = sscanf(files[i]->d_name, host_template, &host_no);
			nic_t *nic;

			LOG_INFO(PFX "Found host[%d]: %s",
				 host_no, files[i]->d_name);

			/*  Build the path to determine netdev name */
			snprintf(temp_path, sizeof(temp_path),
				 iscsi_host_path_netdev_template, host_no);

			rc = capture_file(&raw, &raw_size, temp_path);
			if (rc != 0)
				continue;

			rc = from_host_no_find_associated_eth_device(host_no,
								     &nic);
			if (rc != 0) {
				/*  Normalize the string */
				if (raw[raw_size - 1] == '\n')
					raw[raw_size - 1] = '\0';

				nic = nic_init();
				if (nic == NULL) {
					LOG_ERR(PFX "Couldn't allocate "
						"space for NIC %s "
						"during scan", raw);

					free(raw);
					rc = -ENOMEM;
					break;
				}

				strncpy(nic->eth_device_name, raw, raw_size);
				nic->config_device_name = nic->eth_device_name;
				nic->log_name = nic->eth_device_name;
				nic->host_no = host_no;

				if (nic_fill_name(nic) != 0) {
					free(nic);
					free(raw);
					rc = -EIO;
					continue;
				}

				nic_add(nic);

				LOG_INFO(PFX "NIC not found creating an "
					 "instance for host_no: %d %s",
					 host_no, nic->eth_device_name);
			} else
				LOG_INFO(PFX "%s: NIC found host_no: %d",
					 nic->log_name, host_no);

			free(raw);
		}
		pthread_mutex_unlock(&nic_list_mutex);

		/*  Cleanup the scandir() call */
		for (i = 0; i < count; i++)
			free(files[i]);
		free(files);

		rc = 0;
		break;
	}

	return rc;
}

/******************************************************************************
 *  Enable/Disable Multicast on physical interface
 *****************************************************************************/
static int nic_util_enable_disable_multicast(nic_t *nic, uint32_t cmd)
{
	int rc = 0;
	struct uip_eth_addr multicast_addr;
	int fd;
	struct ifreq ifr;

	/* adding ethernet multicast address for IPv6 */
	memcpy(&multicast_addr, nic->mac_addr, ETH_ALEN);
	multicast_addr.addr[0] = 0x33;
	multicast_addr.addr[1] = 0x33;
	multicast_addr.addr[2] = 0xff;

	/* Prepare the request */
	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, nic->eth_device_name,
		sizeof(ifr.ifr_name));
	memcpy(ifr.ifr_hwaddr.sa_data, multicast_addr.addr, ETH_ALEN);

	fd = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd < 0) {
		LOG_ERR(PFX "%s: Couldn't create socket to %s "
			"multicast address: %s",
			nic->log_name,
			cmd == SIOCADDMULTI ? "added" : "delete",
			strerror(errno));
		return errno;
	}

	rc = fcntl(fd, F_SETFL, O_NONBLOCK);
	if (rc != 0) {
		LOG_WARN("%s: Couldn't set to ethtool IOCTL to "
			 "non-blocking [%s]", nic->log_name, strerror(errno));
	}

	if (ioctl(fd, cmd, (char *)&ifr) != 0) {
		LOG_ERR("%s: Couldn't issue ioctl socket to %s "
			"multicast address: %s",
			nic->log_name,
			cmd == SIOCADDMULTI ? "add" : "delete",
			strerror(errno));
		rc = errno;
		goto error;
	}

	LOG_INFO(PFX "%s: %s address %02x:%02x:%02x:%02x:%02x:%02x "
		 "to multicast list",
		 nic->log_name,
		 cmd == SIOCADDMULTI ? "Added" : "Deleted",
		 multicast_addr.addr[0], multicast_addr.addr[1],
		 multicast_addr.addr[2], multicast_addr.addr[3],
		 multicast_addr.addr[4], multicast_addr.addr[5]);

	if (cmd == SIOCADDMULTI)
		nic->flags |= NIC_ADDED_MULICAST;
	else
		nic->flags &= ~NIC_ADDED_MULICAST;

error:
	close(fd);

	return rc;
}

/**
 *  enable_multicast() - This fuction is used to enable
 *	the listening of multicast addresses for a given network interface
 *  @param nic - NIC device to enable multicast on
 *  @return 0 for success or <0 for failure
 */
int enable_multicast(nic_t *nic)
{
	return nic_util_enable_disable_multicast(nic, SIOCADDMULTI);
}

/**
 *  disable_multicast() - This fuction is used to disable
 *	the listening of multicast addresses for a given network interface
 *  @param dev - NIC  device to disable multicast on
 *  @return 0 for success or <0 for failure
 */
int disable_multicast(nic_t *nic)
{
	return nic_util_enable_disable_multicast(nic, SIOCDELMULTI);
}

/*******************************************************************************
 * Finding associated UIO/physical network interfaces
 ******************************************************************************/
static int filter_net_name(const struct dirent *entry)
{
	if ((memcmp(entry->d_name, "net:", 4) == 0))
		return 1;
	else
		return 0;
}

static char *extract_net_name(struct dirent **files)
{
	return strstr(files[0]->d_name, ":");
}

static int filter_dot_out(const struct dirent *entry)
{
	if ((memcmp(entry->d_name, ".", 1) == 0))
		return 0;
	else
		return 1;
}

static char *extract_none(struct dirent **files)
{
	return files[0]->d_name;
}

/**
 *  from_host_no_find_nic() - Given the host number
 *      this function will try to find the assoicated nic interface
 *  Must be called with nic_list_mutex lock
 *  @param host_no - minor number of the UIO device
 *  @param nic - pointer to the NIC will set if successful
 *  @return 0 on success, <0 on error
 */
int from_host_no_find_associated_eth_device(int host_no, nic_t **nic)
{
	nic_t *current_nic = nic_list;
	char *raw = NULL, *raw_tmp;
	uint32_t raw_size = 0;

	char temp_path[sizeof(iscsi_host_path_netdev_template) + 8];
	int rc = -EIO;

	/*  Build the path to determine uio name */
	snprintf(temp_path, sizeof(temp_path),
		 iscsi_host_path_netdev_template, host_no);

	rc = capture_file(&raw, &raw_size, temp_path);
	if (rc != 0)
		goto error;

	/* sanitize name string by replacing newline with null termination */
	raw_tmp = raw;
	while (*raw_tmp != '\n' && raw_size--)
		raw_tmp++;
	*raw_tmp = '\0';

	rc = -EIO;

	current_nic = nic_list;
	while (current_nic != NULL) {
		if (strcmp(raw, current_nic->eth_device_name) == 0) {
			*nic = current_nic;
			rc = 0;
			break;
		}

		current_nic = current_nic->next;
	}

	free(raw);

error:
	return rc;
}

/*******************************************************************************
 *  NIC packet handling functions
 ******************************************************************************/
/**
 *  from_uio_find_associated_eth_device() - Given the uio minor number
 *      this function will try to find the assoicated phyisical network
 *      interface
 *  @param uio_minor - minor number of the UIO device
 *  @param name - char buffer which will be filled if successful
 *  @param name_size - size of the name buffer
 *  @return >0 minor number <0 an error
 */
static int from_uio_find_associated_eth_device(nic_t *nic,
					       int uio_minor,
					       char *name, size_t name_size)
{
	char *path;
	int rc;
	int count;
	struct dirent **files;
	char *parsed_name;
	int i;
	int path_iterator;
	char *search_paths[] = { "/sys/class/uio/uio%i/device/",
		"/sys/class/uio/uio%i/device/net"
	};
	int path_to[] = { 5, 1 };
	int (*search_filters[]) (const struct dirent *) = {
	filter_net_name, filter_dot_out,};
	char *(*extract_name[]) (struct dirent **files) = {
	extract_net_name, extract_none,};
	int extract_name_offset[] = { 1, 0 };

	path = malloc(PATH_MAX);
	if (path == NULL) {
		LOG_ERR(PFX "Could not allocate memory for path");
		rc = -ENOMEM;
		goto error;
	}

	for (path_iterator = 0;
	     path_iterator < sizeof(search_paths) / sizeof(search_paths[0]);
	     path_iterator++) {
		/*  Build the path to determine uio name */
		rc = sprintf(path, search_paths[path_iterator], uio_minor);

		wait_for_file_node_timed(nic, path, path_to[path_iterator]);

		count = scandir(path, &files,
				search_filters[path_iterator], alphasort);

		switch (count) {
		case 1:
			parsed_name = (*extract_name[path_iterator]) (files);
			if (parsed_name == NULL) {
				LOG_WARN(PFX "Couldn't find delimiter in: %s",
					 files[0]->d_name);

				break;
			}

			strncpy(name,
				parsed_name +
				extract_name_offset[path_iterator], name_size);

			free(files[0]);
			free(files);

			rc = 0;
			break;

		case 0:
			rc = -EINVAL;
			break;

		case -1:
			LOG_WARN(PFX "Error when scanning path: %s[%s]",
				 path, strerror(errno));
			rc = -EINVAL;
			break;

		default:
			LOG_WARN(PFX
				 "Too many entries when looking for device: %s",
				 path);

			/*  Cleanup the scandir() call */
			for (i = 0; i < count; i++)
				free(files[i]);
			free(files);

			rc = -EINVAL;
			break;
		}

		if (rc == 0)
			break;
	}

error:
	free(path);

	return rc;
}

/**
 *  from_uio_find_associated_host() - Given the uio minor number
 *      this function will try to find the assoicated iscsi host
 *  @param uio_minor - minor number of the UIO device
 *  @param name - char buffer which will be filled if successful
 *  @param name_size - size of the name buffer
 *  @return >0 minor number <0 an error
 */
static int from_uio_find_associated_host(nic_t *nic, int uio_minor,
					 char *name, size_t name_size)
{
	char *path;
	int rc;
	int count;
	struct dirent **files;
	char *parsed_name;
	int i;
	int path_iterator;
	char *search_paths[] = { "/sys/class/uio/uio%i/device/" };
	int path_to[] = { 5, 1 };
	int (*search_filters[]) (const struct dirent *) = { filter_host_name, };
	char *(*extract_name[]) (struct dirent **files) = {  extract_none, };
	int extract_name_offset[] = { 0 };

	path = malloc(PATH_MAX);
	if (!path) {
		LOG_ERR(PFX "Could not allocate memory for path");
		rc = -ENOMEM;
		goto error;
	}

	for (path_iterator = 0;
	     path_iterator < sizeof(search_paths) / sizeof(search_paths[0]);
	     path_iterator++) {
		/*  Build the path to determine uio name */
		rc = sprintf(path, search_paths[path_iterator], uio_minor);

		wait_for_file_node_timed(nic, path, path_to[path_iterator]);

		count = scandir(path, &files,
				search_filters[path_iterator], alphasort);

		switch (count) {
		case 1: {
			char *parsed_src;
			size_t parsed_size;

			parsed_name = (*extract_name[path_iterator]) (files);
			if (!parsed_name) {
				LOG_WARN(PFX "Couldn't find delimiter in: %s",
					 files[0]->d_name);

				break;
			}

			parsed_src = parsed_name + extract_name_offset[path_iterator];
			parsed_size = strlen(parsed_src);
			if (parsed_size >= name_size) {
				LOG_WARN(PFX "uio device name too long: %s (max %d)",
					parsed_src, (int)name_size - 1);
				rc = -EINVAL;
			} else {
				strncpy(name, parsed_src, name_size);
				rc = 0;
			}

			free(files[0]);
			free(files);

			break;
		}

		case 0:
			rc = -EINVAL;
			break;

		case -1:
			LOG_WARN(PFX "Error when scanning path: %s[%s]",
				 path, strerror(errno));
			rc = -EINVAL;
			break;

		default:
			LOG_WARN(PFX
				 "Too many entries when looking for device: %s",
				 path);

			/*  Cleanup the scandir() call */
			for (i = 0; i < count; i++)
				free(files[i]);
			free(files);

			rc = -EINVAL;
			break;
		}

		if (rc == 0)
			break;
	}

error:
	free(path);

	return rc;
}

/**
 *  filter_uio_name() - This is the callback used by scandir when looking for
 *                      the number of uio entries
 */
static int filter_uio_name(const struct dirent *entry)
{
	/*  Only return if the name of the file begins with 'uio' */
	if ((memcmp(entry->d_name, uio_name, sizeof(uio_name) - 1) == 0))
		return 1;
	else
		return 0;
}

/**
 * from_netdev_name_find_nic() - This is used to find the NIC device given
 *                               the netdev name
 * @param interface_name - name of the interface to search on
 * @param nic - pointer of the pointer to the NIC
 * @return 0 on success, <0 on failure
 */
int from_netdev_name_find_nic(char *interface_name, nic_t **nic)
{
	nic_t *current_nic;

	current_nic = nic_list;
	while (current_nic != NULL) {
		if (strcmp(interface_name, current_nic->eth_device_name) == 0)
			break;

		current_nic = current_nic->next;
	}

	if (current_nic == NULL)
		return -EINVAL;

	*nic = current_nic;
	return 0;
}

/**
 *  from_phys_name_find_assoicated_uio_device() - This is used to find the
 *						  uio minor
 *      when given a network interface name
 *  @param interface_name - network interface name to search for
 *  @return >0 minor number <0 an error
 */
int from_phys_name_find_assoicated_uio_device(nic_t *nic)
{
	char *path = NULL;
	int count;
	struct dirent **files;
	int i;
	int rc;
	char *interface_name = nic->config_device_name;

	if (interface_name == NULL)
		interface_name = nic->eth_device_name;

	/*  Wait at least 10 seconds for uio sysfs entries to appear */
	rc = wait_for_file_node_timed(nic, (char *)base_uio_sysfs_name, 10);
	if (rc != 0)
		return rc;

	count = scandir(base_uio_sysfs_name,
			&files, filter_uio_name, alphasort);

	switch (count) {
	case 0:
		LOG_WARN(PFX "Couldn't find %s to determine uio minor",
			 interface_name);
		return -EINVAL;

	case -1:
		LOG_WARN(PFX "Error when scanning for %s in path: %s [%s]",
			 interface_name, base_uio_sysfs_name, strerror(errno));
		return -EINVAL;
	}

	path = malloc(PATH_MAX);
	if (path == NULL) {
		LOG_ERR(PFX "Could not allocate memory for path");
		return -ENOMEM;
	}

	/*  Run through the contents of the filtered files to see if the
	 *  network interface name matches that of the uio device */
	for (i = 0; i < count; i++) {
		int uio_minor;
		char eth_name[IFNAMSIZ];

		rc = sscanf(files[i]->d_name, "uio%d", &uio_minor);
		if (rc != 1) {
			LOG_WARN("Could not parse: %s", files[i]->d_name);
			continue;
		}

		if (!memcmp(host_pfx, nic->config_device_name,
			    strlen(host_pfx))) {
			rc = from_uio_find_associated_host(nic, uio_minor,
							   eth_name,
							   sizeof(eth_name));
		} else {
			rc = from_uio_find_associated_eth_device(nic, uio_minor,
								 eth_name,
								 sizeof(eth_name));
		}
		if (rc != 0) {
			LOG_WARN("uio minor: %d not valid [%D]", uio_minor, rc);
			continue;
		}

		if (strncmp(eth_name, interface_name, sizeof(eth_name)) == 0) {
			memcpy(nic->eth_device_name,
			       eth_name, sizeof(nic->eth_device_name));

			LOG_INFO(PFX "%s associated with uio%d",
				 nic->eth_device_name, uio_minor);

			rc = uio_minor;
			goto done;
		}
	}

	LOG_WARN("Could not find assoicate uio device with %s", interface_name);

	rc = -EINVAL;
done:
	if (path != NULL)
		free(path);

	for (i = 0; i < count; i++)
		free(files[i]);
	free(files);

	return rc;

}

/**
 *  nic_verify_uio_sysfs_name() - Using the name entry in sysfs it will try to
 *      match the NIC library name
 *  @param nic - The NIC hardware to check
 *
 */
int nic_verify_uio_sysfs_name(nic_t *nic)
{
	char *raw = NULL, *raw_tmp;
	uint32_t raw_size = 0;
	char temp_path[sizeof(nic_uio_sysfs_name_tempate) + 8];
	int rc = 0;
	nic_lib_handle_t *handle = NULL;
	size_t name_size;


	/*  Build the path to determine uio name */
	snprintf(temp_path, sizeof(temp_path),
		 nic_uio_sysfs_name_tempate, nic->uio_minor);

	rc = capture_file(&raw, &raw_size, temp_path);
	if (rc != 0)
		goto error;

	/* sanitize name string by replacing newline with null termination */
	raw_tmp = raw;
	while (*raw_tmp != '\n' && raw_size--)
		raw_tmp++;
	*raw_tmp = '\0';

	/*  If the nic library is not set then check if there is a library
	 *  which matches the uio sysfs name */
	if (nic->nic_library == NULL) {
		NIC_LIBRARY_EXIST_T exist;

		exist = does_nic_uio_name_exist(raw, &handle);
		if (exist == NIC_LIBRARY_DOESNT_EXIST) {
			LOG_ERR(PFX "%s: could not find library for uio name: %s",
				nic->log_name, raw);
			rc = -EINVAL;
			goto error;
		}

		/* fill the lib info */
		nic->nic_library = handle;
		nic->ops = handle->ops;
		(*nic->ops->lib_ops.get_library_name) (&nic->library_name,
						       &name_size);
	} else {
		/*  Get the uio sysfs name from the NIC library */
		(*nic->ops->lib_ops.get_uio_name) (&raw_tmp, &name_size);

		if (strncmp(raw, raw_tmp, name_size) != 0) {
			LOG_ERR(PFX "%s: uio names not equal: "
				"expecting %s got %s from %s",
				nic->log_name, raw, raw_tmp, temp_path);
			rc = -EINVAL;
			goto error;
		}
	}

	LOG_INFO(PFX "%s: Verified uio name %s with library %s",
		 nic->log_name, raw, nic->library_name);

error:
	if (raw)
		free(raw);

	return rc;
}

/**
 * nic_fill_name() - This will initialize all the hardware resources underneath
 *                   a struct cnic_uio device
 * @param nic - The nic device to attach the hardware with
 * @return 0 on success, on failure a errno will be returned
 */
int nic_fill_name(nic_t *nic)
{
	int rc;

	if ((nic->config_device_name != NULL) &&
	    (memcmp(uio_base_dir, nic->config_device_name,
		    sizeof(uio_base_dir) - 1) == 0)) {
		uint16_t uio_minor;
		char eth_name[sizeof(nic->eth_device_name)];

		wait_for_file_node_timed(nic, nic->config_device_name, 5);

		/*  Determine the minor number for the UIO device */
		rc = sscanf(nic->config_device_name, uio_udev_path_template,
			    &uio_minor);
		if (rc != 1) {
			LOG_WARN(PFX "%s: Could not parse for minor number",
				 nic->uio_device_name);
			return -EINVAL;
		} else
			nic->uio_minor = uio_minor;

		nic->uio_device_name = nic->config_device_name;

		/*  Determine the assoicated physical network interface */
		rc = from_uio_find_associated_eth_device(nic,
							 nic->uio_minor,
							 eth_name,
							 sizeof(eth_name));
		if (rc != 0) {
			LOG_WARN(PFX "%s: Couldn't find associated eth device",
				 nic->uio_device_name);
		} else {
			memcpy(nic->eth_device_name,
			       eth_name, sizeof(eth_name));
		}

		LOG_INFO(PFX "%s: configured for uio device for %s",
			 nic->log_name, nic->uio_device_name);

	} else {
		LOG_INFO(PFX "looking for uio device for %s",
			 nic->config_device_name);

		rc = from_phys_name_find_assoicated_uio_device(nic);
		if (rc < 0) {
			LOG_ERR(PFX "Could not determine UIO name for %s",
				nic->config_device_name);

			return -rc;
		}

		nic->uio_minor = rc;

		if (nic->flags & NIC_UIO_NAME_MALLOC)
			free(nic->uio_device_name);

		nic->uio_device_name =
		    malloc(sizeof(uio_udev_path_template) + 8);
		if (nic->uio_device_name == NULL) {
			LOG_INFO(PFX "%s: Couldn't malloc space for uio name",
				 nic->log_name);
			return -ENOMEM;
		}

		snprintf(nic->uio_device_name,
			 sizeof(uio_udev_path_template) + 8,
			 uio_udev_path_template, nic->uio_minor);

		nic->flags |= NIC_UIO_NAME_MALLOC;
	}

	return 0;
}

void cnic_get_sysfs_pci_resource_path(nic_t *nic, int resc_no,
				      char *sys_path, size_t size)
{
	/*  Build the path to sysfs pci resource */
	snprintf(sys_path, size,
		 cnic_uio_sysfs_resc_template, nic->uio_minor, resc_no);

}

void prepare_library(nic_t *nic)
{
	int rc;
	NIC_LIBRARY_EXIST_T exist;
	nic_lib_handle_t *handle = NULL;

	nic_fill_name(nic);

	/* No assoicated library, we can skip it */
	if (nic->library_name != NULL) {
		/*  Check that we have the proper NIC library loaded */
		exist = does_nic_library_exist(nic->library_name, &handle);
		if (exist == NIC_LIBRARY_DOESNT_EXIST) {
			LOG_ERR(PFX "NIC library doesn't exists: %s",
				nic->library_name);
			goto error;
		} else if (handle && (nic->nic_library == handle) &&
			  (nic->ops == handle->ops)) {
			LOG_INFO("%s: Have NIC library '%s'",
				 nic->log_name, nic->library_name);
		}
	}

	/*  Verify the NIC library to use */
	rc = nic_verify_uio_sysfs_name(nic);
	if (rc != 0) {
		/*  Determine the NIC library to use based on the PCI Id */
		rc = find_set_nic_lib(nic);
		if (rc != 0) {
			LOG_ERR(PFX "%s: Couldn't find NIC library",
				nic->log_name);
			goto error;
		}

	}

	LOG_INFO("%s: found NIC with library '%s'",
		 nic->log_name, nic->library_name);
error:
	return;
}

void prepare_nic_thread(nic_t *nic)
{
	pthread_attr_t attr;
	int rc;

	pthread_mutex_lock(&nic->nic_mutex);
	if (nic->thread == INVALID_THREAD) {
		struct timespec ts;
		struct timeval tp;

		LOG_INFO(PFX "%s: spinning up thread for nic", nic->log_name);

		/*  Try to spin up the nic thread */
		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
		rc = pthread_create(&nic->thread, &attr, nic_loop, nic);
		if (rc != 0) {
			LOG_ERR(PFX "%s: Couldn't create thread for nic",
				nic->log_name);
			goto error;
		}

		/* Convert from timeval to timespec */
		rc = gettimeofday(&tp, NULL);
		ts.tv_sec = tp.tv_sec;
		ts.tv_nsec = tp.tv_usec * 1000;
		ts.tv_sec += 5;	/*  TODO: hardcoded wait for 5 seconds */

		/*  Wait for the nic loop thread to to running */
		rc = pthread_cond_timedwait(&nic->nic_loop_started_cond,
					    &nic->nic_mutex, &ts);

		LOG_INFO("Created nic thread: %s", nic->log_name);
	}

	pthread_mutex_unlock(&nic->nic_mutex);

error:
	return;
}

/*******************************************************************************
 * Functions used to enable/disable the NIC
 ******************************************************************************/
/**
 *  nic_enable() - Function used to enable the NIC
 *  @param nic - NIC to enable
 *  @return 0 on success, <0 on failure
 */
int nic_enable(nic_t *nic)
{
	if (nic->flags & NIC_GOING_DOWN) {
		LOG_INFO(PFX "%s: NIC device is going down, "
			 "flag: 0x%x state: 0x%x",
			 nic->log_name, nic->flags, nic->state);
		return -EINVAL;
	}
	if (nic->state == NIC_STOPPED) {
		struct timespec ts;
		struct timeval tp;
		int rc;

		pthread_mutex_lock(&nic->nic_mutex);
		/*  Signal the device to enable itself */
		pthread_cond_broadcast(&nic->enable_wait_cond);

		nic->flags &= ~NIC_DISABLED;
		nic->flags |= NIC_ENABLED;
		nic->flags |= NIC_ENABLED_PENDING;

		/* Convert from timeval to timespec */
		rc = gettimeofday(&tp, NULL);
		ts.tv_sec = tp.tv_sec;
		ts.tv_nsec = tp.tv_usec * 1000;
		ts.tv_sec += 100;

		/*  Wait for the device to be enabled */
		rc = pthread_cond_timedwait(&nic->enable_done_cond,
					    &nic->nic_mutex, &ts);
		if (rc == 0 && nic->flags & NIC_ENABLED) {
			LOG_DEBUG(PFX "%s: device enabled", nic->log_name);
		} else {
			nic->flags &= ~NIC_ENABLED;
			nic->flags |= NIC_DISABLED;
			nic->flags &= ~NIC_ENABLED_PENDING;

			LOG_ERR(PFX "%s: waiting to finish nic_enable err: %s",
				nic->log_name, strerror(rc));
		}
		pthread_mutex_unlock(&nic->nic_mutex);

		return rc;
	} else {
		LOG_INFO(PFX "%s: device already enabled: "
			 "flag: 0x%x state: 0x%x",
			 nic->log_name, nic->flags, nic->state);
		return -EALREADY;
	}
}

/**
 *  nic_disable() - Function used to disable the NIC
 *  @param nic - NIC to disble
 *  @return void
 */
void nic_disable(nic_t *nic, int going_down)
{
	if (nic->state == NIC_STARTED_RUNNING ||
	    nic->state == NIC_RUNNING) {
		struct timespec ts;
		struct timeval tp;
		int rc;

		/*  Wait for the device to be disabled */
		pthread_mutex_lock(&nic->nic_mutex);

		nic->flags &= ~NIC_ENABLED;
		nic->flags |= NIC_DISABLED;
		nic->flags &= ~NIC_STARTED_RUNNING;
		nic->state = NIC_STOPPED;

		if (going_down)
			nic->flags |= NIC_GOING_DOWN;

		/* Convert from timeval to timespec */
		rc = gettimeofday(&tp, NULL);
		if (rc) {
			LOG_ERR("gettimeofday failed, should never happen: %d\n", errno);
			pthread_mutex_unlock(&nic->nic_mutex);
			return;
		}

		ts.tv_sec = tp.tv_sec;
		ts.tv_nsec = tp.tv_usec * 1000;
		ts.tv_sec += 5;	/*  TODO: hardcoded wait for 5 seconds */

		/*  Wait for the device to be disabled */
		rc = pthread_cond_timedwait(&nic->disable_wait_cond,
					    &nic->nic_mutex, &ts);
		if (rc) {
			LOG_ERR("cond_timedwait failed, should never happen: %d\n", errno);
		}

		pthread_mutex_unlock(&nic->nic_mutex);

		LOG_DEBUG(PFX "%s: device disabled", nic->log_name);

	} else {
		LOG_WARN(PFX "%s: device already disabled: "
			 "flag: 0x%x state: 0x%x",
			 nic->log_name, nic->flags, nic->state);
	}
}

void nic_close_all()
{
	nic_t *nic;

	pthread_mutex_lock(&nic_list_mutex);

	/*  Start the shutdown process */
	nic = nic_list;
	while (nic != NULL) {
		pthread_mutex_lock(&nic->nic_mutex);
		nic_close(nic, 1, FREE_ALL_STRINGS);
		pthread_mutex_unlock(&nic->nic_mutex);

		nic = nic->next;
	}
	pthread_mutex_unlock(&nic_list_mutex);

	LOG_INFO(PFX "All NICs closed");
}

void nic_remove_all()
{
	nic_t *nic, *nic_next;

	pthread_mutex_lock(&nic_list_mutex);

	/*  Start the shutdown process */
	nic = nic_list;
	while (nic != NULL) {
		nic_next = nic->next;
		pthread_mutex_lock(&nic->nic_mutex);
		nic_close(nic, 1, FREE_ALL_STRINGS);
		pthread_mutex_unlock(&nic->nic_mutex);
		nic_remove(nic);
		nic = nic_next;
	}
	pthread_mutex_unlock(&nic_list_mutex);

	LOG_INFO(PFX "All NICs removed");
}


/******************************************************************************
 *  Routines to read initialized UIO values from sysfs
 *****************************************************************************/
/**
 * determine_initial_uio_events() - This utility function will
 *    determine the number of uio events that have occured on the
 *    given device.  This value is read from the UIO sysfs entry
 * @param dev - device to read from
 * @param num_of_event - number of UIO events
 * @return 0 is success, <0 failure
 */
int detemine_initial_uio_events(nic_t *nic, uint32_t *num_of_events)
{
	char *raw = NULL;
	uint32_t raw_size = 0;
	ssize_t elements_read;
	char temp_path[sizeof(cnic_sysfs_uio_event_template) + 8];
	int rc;

	/*  Capture RX buffer size */
	snprintf(temp_path, sizeof(temp_path),
		 cnic_sysfs_uio_event_template, nic->uio_minor);

	rc = capture_file(&raw, &raw_size, temp_path);
	if (rc != 0)
		goto error;

	elements_read = sscanf(raw, "%d", num_of_events);
	if (elements_read != 1) {
		LOG_ERR(PFX "%s: Couldn't parse UIO events size from %s",
			nic->log_name, temp_path);
		rc = -EIO;
		goto error;
	}

	rc = 0;
error:
	if (raw)
		free(raw);

	return rc;
}

int get_iscsi_transport_handle(nic_t *nic, uint64_t *handle)
{
	char *raw = NULL;
	uint32_t raw_size = 0;
	ssize_t elements_read;
	char temp_path[sizeof(iscsi_transport_handle_template) + 8];
	int rc;

	/*  Capture RX buffer size */
	snprintf(temp_path, sizeof(temp_path),
		 iscsi_transport_handle_template, nic->library_name);

	rc = capture_file(&raw, &raw_size, temp_path);
	if (rc != 0)
		goto error;

	elements_read = sscanf(raw, "%" PRIu64, handle);
	if (elements_read != 1) {
		LOG_ERR(PFX "%s: Couldn't parse transport handle from %s",
			nic->log_name, temp_path);
		rc = -EIO;
		goto error;
	}

	rc = 0;
error:
	if (raw != NULL)
		free(raw);

	return rc;
}

/**
 *  nic_set_all_nic_iface_mac_to_parent() - This is a utility function used to
 *      intialize all the MAC addresses of the network interfaces for a given
 *      CNIC UIO device
 *  Call with nic mutex held
 *  @param dev - CNIC UIO device to initialize
 */
void nic_set_all_nic_iface_mac_to_parent(nic_t *nic)
{
	nic_interface_t *current, *vlan_current;

	current = nic->nic_iface;
	while (current != NULL) {
		/*  Set the initial MAC address of this interface to the parent
		 *  adapter */
		memcpy(current->mac_addr, nic->mac_addr, 6);

		vlan_current = current->vlan_next;
		while (vlan_current != NULL) {
			memcpy(vlan_current->mac_addr, nic->mac_addr, 6);
			vlan_current = vlan_current->vlan_next;
		}
		current = current->next;
	}
}

/*******************************************************************************
 *  NIC packet handling functions
 ******************************************************************************/
/**
 *  nic_alloc_packet_buffer() - Used to allocate a packet buffer used to
 *      send a TX packet later
 *  @param nic - nic device to send the packet on
 *  @param nic_iface - nic interface to send out on
 *  @param buf - pointer to the buffer to send
 *  @param buf_size - size in bytes of the buffer to send
 *  @return pointer to the allocated packet buffer
 *          NULL if memory could not be allocated
 */
static packet_t *nic_alloc_packet_buffer(nic_t *nic,
					 nic_interface_t *nic_iface,
					 uint8_t *buf, size_t buf_size)
{
	packet_t *pkt;

	pkt = malloc(sizeof(*pkt) + buf_size);
	if (pkt == NULL) {
		LOG_ERR(PFX "%s: Couldn't allocate space for packet buffer",
			nic->log_name);
		return NULL;
	}

	pkt->next = NULL;
	pkt->nic = nic;
	pkt->nic_iface = nic_iface;
	pkt->buf_size = buf_size;
	memcpy(pkt->buf, buf, buf_size);

	return pkt;
}

/**
 *  nic_queue_tx_packet() - Used to queue a TX packet buffer to send later
 *  @param nic - NIC device to send the packet on
 *  @param nic_iface - NIC interface to send on the packet on
 *  @param pkt - packet to queue
 *  @return 0 if successful or <0 if unsuccessful
 */
int nic_queue_tx_packet(nic_t *nic,
			nic_interface_t *nic_iface, packet_t *pkt)
{
	packet_t *queued_pkt;

	queued_pkt = nic_alloc_packet_buffer(nic, nic_iface,
					     pkt->buf, pkt->buf_size);
	if (queued_pkt == NULL) {
		LOG_ERR(PFX "%s: Couldn't allocate tx packet to queue",
			nic->log_name);
		return -ENOMEM;
	}

	if (nic->tx_packet_queue == NULL) {
		nic->tx_packet_queue = queued_pkt;
	} else {
		packet_t *current_pkt;

		current_pkt = nic->tx_packet_queue;
		while (current_pkt->next != NULL)
			current_pkt = current_pkt->next;

		current_pkt->next = queued_pkt;
	}

	LOG_DEBUG(PFX "%s: tx packet queued", nic->log_name);

	return 0;
}

/**
 *  nic_dequeue_tx_packet() - Used pop a TX packet buffer of the TX
 *  @param dev - cnic_uio device to send the packet on
 *  @param buf - pointer to the buffer to send
 *  @param buf_size - size in bytes of the buffer to send
 *  @return NULL if there are no more TX packet buffers to send
 *	    pointer to the packet buffer which is detached from the device
 */
packet_t *nic_dequeue_tx_packet(nic_t *nic)
{
	packet_t *pkt;

	pkt = nic->tx_packet_queue;

	/* There is a packet buffer to send, time to detach it from the
	 * cnic_uio device */
	if (pkt != NULL) {
		nic->tx_packet_queue = pkt->next;
		pkt->next = NULL;
	}

	return pkt;
}

void nic_fill_ethernet_header(nic_interface_t *nic_iface,
			      void *data,
			      void *src_addr, void *dest_addr,
			      int *pkt_size, void **start_addr,
			      uint16_t ether_type)
{
	struct ether_header *eth;
	uint16_t *vlan_hdr;

	eth = data;

	memcpy(eth->ether_shost, src_addr, ETH_ALEN);
	memcpy(eth->ether_dhost, dest_addr, ETH_ALEN);

	vlan_hdr = (uint16_t *) (eth + 1);
	eth->ether_type = htons(ether_type);

	*start_addr = vlan_hdr;
}

/*******************************************************************************
 *  NIC interface management utility functions
 ******************************************************************************/
/**
 *  nic_find_nic_iface() - This function is used to find an interface
 *                         from the NIC
 *  @param nic - NIC to look for network interfaces
 *  @param vlan_id - VLAN id to look for
 *  @param protocol - either AF_INET or AF_INET6
 *  @param iface_num - iface num to use if present
 *  @param request_type - IPV4/6 DHCP/STATIC
 *  @return nic_iface - if found network interface with the given VLAN ID
 *                      if not found a NULL is returned
 */
nic_interface_t *nic_find_nic_iface(nic_t *nic,
				    uint16_t protocol,
				    uint16_t vlan_id,
				    int iface_num,
				    int request_type)
{
	nic_interface_t *current = nic->nic_iface;
	nic_interface_t *current_vlan = NULL;

	while (current != NULL) {
		LOG_DEBUG(PFX "%s: incoming protocol: %d, vlan_id:%d iface_num: %d, request_type: %d",
			  nic->log_name, protocol, vlan_id, iface_num,  request_type);
		LOG_DEBUG(PFX "%s: host:%d iface_num: 0x%x VLAN: %d protocol: %d",
			  nic->log_name, nic->host_no, current->iface_num, current->vlan_id, current->protocol);
		if (current->protocol != protocol)
			goto next;

		/* Check for iface_num first */
		if (iface_num != IFACE_NUM_INVALID) {
			if (current->iface_num == iface_num) {
				/* Exception is when iface_num == 0, need to
				   check for request_type also if !=
				   IP_CONFIG_OFF */
				if (!iface_num && request_type !=
				    IP_CONFIG_OFF) {
					if (current->request_type ==
					    request_type)
						goto found;
				} else {
					goto found;
				}
			}
		} else if (vlan_id == NO_VLAN) {
			/* Just return the top of the family */
			goto found;
		} else {
			if ((current->vlan_id == vlan_id) &&
			    ((request_type == IP_CONFIG_OFF) ||
			    (current->request_type == request_type)))
				goto found;
		}
		/* vlan_next loop */
		current_vlan = current->vlan_next;
		while (current_vlan != NULL) {
			if (iface_num != IFACE_NUM_INVALID) {
				if (current_vlan->iface_num == iface_num) {
					if (!iface_num && request_type !=
					    IP_CONFIG_OFF) {
						if (current_vlan->request_type
						    == request_type)
							goto vlan_found;
					} else {
						goto vlan_found;
					}
				}
			}
			if ((current_vlan->vlan_id == vlan_id) &&
			    ((request_type == IP_CONFIG_OFF) ||
			    (current_vlan->request_type == request_type)))
				goto vlan_found;

			current_vlan = current_vlan->vlan_next;
		}
next:
		current = current->next;
	}
vlan_found:
	current = current_vlan;
found:
	return current;
}

/* Called with nic mutex held */
void persist_all_nic_iface(nic_t *nic)
{
	nic_interface_t *current_vlan, *current;

	current = nic->nic_iface;
	while (current != NULL) {
		current->flags |= NIC_IFACE_PERSIST;
		current_vlan = current->vlan_next;
		while (current_vlan != NULL) {
			current_vlan->flags |= NIC_IFACE_PERSIST;
			current_vlan = current_vlan->vlan_next;
		}
		current = current->next;
	}
}

/* Sets the nic_iface to the front of the AF */
void set_nic_iface(nic_t *nic, nic_interface_t *nic_iface)
{
	nic_interface_t *current, *prev;
	nic_interface_t *current_vlan, *prev_vlan;

	prev = NULL;
	current = nic->nic_iface;
	while (current != NULL) {
		if (current->protocol != nic_iface->protocol)
			goto next;
		/* If its already on top of the list, exit */
		if (current == nic_iface)
			goto done;

		prev_vlan = current;
		current_vlan = current->vlan_next;

		while (current_vlan != NULL) {
			if (current_vlan == nic_iface) {
				/* Found inside the vlan list */
				/* For vlan == 0, place on top of
				   the AF list */
				prev_vlan->vlan_next =
						current_vlan->vlan_next;
				current_vlan->vlan_next = current;
				if (prev)
					prev->next = current_vlan;
				else
					nic->nic_iface = current_vlan;
				goto done;
			}
			prev_vlan = current_vlan;
			current_vlan = current_vlan->vlan_next;
		}
next:
		prev = current;
		current = current->next;
	}
done:
	return;
}

/*******************************************************************************
 *  Packet management utility functions
 ******************************************************************************/
/**
 *  get_next_packet_in_queue() - This function will return the next packet in
 *    the queue
 *  @param queue - the queue to pull the packet from
 *  @return the packet in the queue
 */
static packet_t *get_next_packet_in_queue(packet_t **queue)
{
	packet_t *pkt;

	if (*queue == NULL)
		return NULL;

	pkt = *queue;
	*queue = pkt->next;

	return pkt;
}

/**
 *  get_next_tx_packet() - This function will return the next packet in
 *    the TX queue
 *  @param nic - NIC to pull the TX packet from
 *  @return the packet in hte queue
 */
packet_t *get_next_tx_packet(nic_t *nic)
{
	return get_next_packet_in_queue(&nic->tx_packet_queue);
}

/**
 *  get_next_free_packet() - This function will return the next packet in
 *    the free queue
 *  @param nic - NIC to pull the RX packet from
 *  @return the packet in hte queue
 */
packet_t *get_next_free_packet(nic_t *nic)
{
	packet_t *pkt;
	pthread_mutex_lock(&nic->free_packet_queue_mutex);
	pkt = get_next_packet_in_queue(&nic->free_packet_queue);
	pthread_mutex_unlock(&nic->free_packet_queue_mutex);

	if (pkt != NULL)
		reset_packet(pkt);

	return pkt;
}

/**
 *  put_packet_in_queue() - This function will place the packet in the given
 *    queue
 *  @param pkt   - the packet to place
 *  @param queue - the queue to place the packet
 *  @return the packet in the queue
 */
static void put_packet_in_queue(packet_t *pkt, packet_t **queue)
{
	if (*queue == NULL)
		*queue = pkt;
	else {
		pkt->next = *queue;
		*queue = pkt;
	}
}

/**
 *  put_packet_in_tx_queue() - This function will place the packet in
 *    the TX queue
 *  @param pkt - packet to place
 *  @param nic - NIC to pull the TX packet from
 *  @return the packet in hte queue
 */
void put_packet_in_tx_queue(packet_t *pkt, nic_t *nic)
{
	return put_packet_in_queue(pkt, &nic->tx_packet_queue);
}

/**
 *  put_packet_in_free_queue() - This function will place the packet in
 *    the RX queue
 *  @param pkt - packet to place
 *  @param nic - NIC to pull the RX packet from
 *  @return the packet in hte queue
 */
void put_packet_in_free_queue(packet_t *pkt, nic_t *nic)
{
	pthread_mutex_lock(&nic->free_packet_queue_mutex);
	put_packet_in_queue(pkt, &nic->free_packet_queue);
	pthread_mutex_unlock(&nic->free_packet_queue_mutex);
}

uint32_t calculate_default_netmask(uint32_t ip_addr)
{
	uint32_t netmask;

	if (IN_CLASSA(ntohl(ip_addr)))
		netmask = htonl(IN_CLASSA_NET);
	else if (IN_CLASSB(ntohl(ip_addr)))
		netmask = htonl(IN_CLASSB_NET);
	else if (IN_CLASSC(ntohl(ip_addr)))
		netmask = htonl(IN_CLASSC_NET);
	else {
		LOG_ERR("Unable to guess netmask for address %x\n", &ip_addr);
		return -1;
	}

	return netmask;
}

void dump_packet_to_log(struct nic_interface *iface,
			uint8_t *buf, uint16_t buf_len)
{

	FILE *file;
	char str[80];
	int i, count;

	file = fmemopen(str, sizeof(str), "w+");
	if (file == NULL) {
		LOG_ERR(PFX "Could not create logging file stream for packet "
			"logging: [%d: %s]", errno, strerror(errno));
		return;
	}

	LOG_PACKET(PFX "%s: Start packet dump len: %d", iface->parent->log_name,
		   buf_len);

	for (i = 0; i < buf_len; i++) {
		rewind(file);
		fprintf(file, "%03x:  ", i);

		for (count = 0; (count < 8) && i < buf_len; count++, i++)
			fprintf(file, " %02x", buf[i]);
		fflush(file);

		LOG_PACKET(PFX "%s: %s", iface->parent->log_name, str);
	}

	LOG_PACKET(PFX "%s: end packet dump", iface->parent->log_name);

	fclose(file);
}

/*******************************************************************************
 *  File Management
 ******************************************************************************/
 /**
  * determine_file_size_read() - when fstat doesn't work on filepath
  *     within the /proc filesytem, we need to read/count the size of the file
  *     until we hit a EOF
  * @parm filepath - path of the file in which to determine the filesize in
  *                  bytes
  * @return file size in bytes, <0 on failure
  */
int determine_file_size_read(const char *filepath)
{
	size_t total_size = 0;
	ssize_t size = 1;
	int fd;
	char buf[1024];

	fd = open(filepath, O_RDONLY);
	if (fd == -1) {
		LOG_ERR("Could not open file: %s [%s]",
			filepath, strerror(errno));
		return -1;
	}

	while (size > 0) {
		size = read(fd, buf, sizeof(buf));

		switch (size) {
		case 0:
			break;
		case -1:
			LOG_ERR("Error reading file: %s [%s]",
				filepath, strerror(errno));
			total_size = -1;
			break;
		default:
			total_size += size;
			break;
		}
	}

	close(fd);

	return total_size;
}

/**
 *  capture_file() - Used to capture a file into a buffer
 *  @param raw - This pointer will be set to the buffer which will hold the
 *         file contents
 *  @param raw_size - This is the size of the buffer returned
 *  @param path - The file path to capture the data from
 *  @return 0 is returned on success, <0 is returned on failure
 */
int capture_file(char **raw, uint32_t *raw_size, const char *path)
{
	FILE *fp;
	size_t read_size;
	int rc = 0;
	int file_size;

	file_size = determine_file_size_read(path);
	if (file_size < 0) {
		LOG_ERR("Could not determine size %s", path);
		return -EIO;
	}

	fp = fopen(path, "r");
	if (fp == NULL) {
		LOG_ERR("Could not open path %s [%s]", path, strerror(errno));
		return -EIO;
	}

	*raw = malloc(file_size);
	if (*raw == NULL) {
		LOG_ERR("Could not malloc space for capture %s", path);
		rc = -ENOMEM;
		goto error;
	}

	read_size = fread(*raw, file_size, 1, fp);
	if (!read_size) {
		LOG_ERR("Could not read capture, path: %s len: %d [%s]",
			path, file_size, strerror(ferror(fp)));
		free(*raw);
		*raw = NULL;
		rc = errno;
	} else
		*raw_size = file_size;

error:
	fclose(fp);

	LOG_INFO("Done capturing %s", path);

	return rc;
}