-
Notifications
You must be signed in to change notification settings - Fork 11
/
fcoemon.c
3902 lines (3422 loc) · 92.3 KB
/
fcoemon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 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", 1, NULL, 'd'},
{"legacy", 0, NULL, 'l'},
{"syslog", 1, 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 = 8192; /* initial size */
/*
* 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));
file[sizeof(file) - 1] = '\0';
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 int log_nlmsg_error(struct nlmsghdr *hp, size_t rlen, const char *str)
{
struct nlmsgerr *ep;
if (NLMSG_OK(hp, rlen)) {
ep = (struct nlmsgerr *)NLMSG_DATA(hp);
FCM_LOG_DBG("%s, err=%d, type=%d\n",
str, ep->error, ep->msg.nlmsg_type);
return ep->error;
} else {
FCM_LOG("%s", str);
return 0;
}
}
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];
struct fcoe_port *p;
static const int flags[] = {
[false] = CLIF_FLAGS_FABRIC,
[true] = CLIF_FLAGS_VN2VN,
};
int rc;
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);
rc = snprintf(vlan_name, sizeof(vlan_name), FCOE_VLAN_FORMAT,
real_name, vid);
if (rc < 0 || (size_t) rc >= sizeof(vlan_name)) {
FCM_LOG("Warning: Generating FCoE VLAN device name for"
"interface %s VLAN %d: format resulted in a"
"name larger than IFNAMSIZ\n", real_name, vid);
vlan_name[sizeof(vlan_name) - 1] = 0;
FCM_LOG("\tTruncating VLAN name to %s\n", vlan_name);
}
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) {