-
Notifications
You must be signed in to change notification settings - Fork 2
/
fcoe.c
173 lines (158 loc) · 4.88 KB
/
fcoe.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
/*
* Copyright (C) 2010 Michael Brown <[email protected]>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <string.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <pcap.h>
#include <syslog.h>
#include <endian.h>
#include "list.h"
#include "fc.h"
#include "fcoe.h"
#include "fcds.h"
#include "fcoed.h"
#define CRCPOLY 0xedb88320
/**
* Calculate 32-bit little-endian CRC checksum
*
* @v seed Initial value
* @v data Data to checksum
* @v len Length of data
*
* Usually @a seed is initially zero or all one bits, depending on the
* protocol. To continue a CRC checksum over multiple calls, pass the
* return value from one call as the @a seed parameter to the next.
*/
static uint32_t crc32_le ( uint32_t seed, const void *data, size_t len ) {
uint32_t crc = seed;
const uint8_t *src = data;
uint32_t mult;
int i;
while ( len-- ) {
crc ^= *src++;
for ( i = 0; i < 8; i++ ) {
mult = ( crc & 1 ) ? CRCPOLY : 0;
crc = ( crc >> 1 ) ^ mult;
}
}
return crc;
}
/**
* Transmit FCoE frame
*
* @v fchdr Fibre Channel frame
* @v len Length of Fibre Channel frame
* @ret rc Return status code
*/
int fc_tx ( struct fc_frame_header *fchdr, size_t len ) {
struct fc_port_id *dest_id = &fchdr->d_id;
struct fcoed_interface *dest_intf;
struct fcoed_port *dest_port;
struct {
struct ethhdr ethhdr;
struct vlan_header vlan;
struct fcoe_header fcoehdr;
char fc[len];
struct fcoe_footer fcoeftr;
} __attribute__ (( packed )) data;
struct ethhdr *ethhdr = &data.ethhdr;
uint32_t crc;
size_t data_len = sizeof ( data );
size_t sent_len;
/* Identify destination interface and port */
if ( find_port_by_id ( dest_id, &dest_intf, &dest_port ) < 0 ) {
logmsg ( LOG_WARNING, "cannot transmit to unknown ID "
FC_PORT_ID_FMT "\n", FC_PORT_ID_ARGS ( dest_id ) );
return -1;
}
/* Calculate CRC */
crc = crc32_le ( ~((uint32_t)0), fchdr, len );
/* Build complete data */
if ( ! dest_port->vlan ) {
ethhdr = ( ( ( void * ) ethhdr ) + sizeof ( data.vlan ) );
data_len -= sizeof ( data.vlan );
}
memcpy ( ethhdr->h_dest, dest_port->mac, sizeof ( ethhdr->h_dest ) );
memcpy ( ethhdr->h_source, fc_f_mac, sizeof ( ethhdr->h_source ) );
ethhdr->h_proto = htons ( ETH_P_FCOE );
if ( dest_port->vlan ) {
data.vlan.tci = dest_port->vlan;
data.vlan.net_proto = ethhdr->h_proto;
ethhdr->h_proto = ntohs ( ETH_P_8021Q );
}
memset ( &data.fcoehdr, 0, sizeof ( data.fcoehdr ) );
data.fcoehdr.sof = ( ( fchdr->seq_cnt == ntohs ( 0 ) ) ?
FCOE_SOF_I3 : FCOE_SOF_N3 );
memcpy ( data.fc, fchdr, sizeof ( data.fc ) );
memset ( &data.fcoeftr, 0, sizeof ( data.fcoeftr ) );
data.fcoeftr.crc = htole32 ( crc ^ ~((uint32_t)0) );
data.fcoeftr.eof = ( ( fchdr->f_ctl_es & FC_F_CTL_ES_END ) ?
FCOE_EOF_T : FCOE_EOF_N );
/* Send packet */
sent_len = pcap_inject ( dest_intf->pcap, ethhdr, data_len );
if ( sent_len != data_len ) {
logmsg ( LOG_ERR, "could not forward to %s: %s\n",
dest_intf->name, pcap_geterr ( dest_intf->pcap ) );
return -1;
}
return 0;
}
/**
* Receive FCoE packet
*
* @v intf Interface
* @v vlan VLAN tag, if present
* @v src Source address
* @v data Data
* @v len Length of data
* @ret rc Return status code
*/
int fcoe_rx ( struct fcoed_interface *intf __unused, uint16_t vlan __unused,
uint8_t *src __unused, void *data, size_t len ) {
struct fcoe_header *fcoehdr;
struct fcoe_footer *fcoeftr;
struct fc_frame_header *fchdr;
struct fc_port_id *dest_id;
/* Strip FCoE header and footer */
if ( len < sizeof ( *fcoehdr ) ) {
logmsg ( LOG_ERR, "received truncated FCoE frame\n" );
return -1;
}
fcoehdr = data;
data += sizeof ( *fcoehdr );
len -= sizeof ( *fcoehdr );
if ( len < sizeof ( *fcoeftr ) ) {
logmsg ( LOG_ERR, "received truncated FCoE frame\n" );
return -1;
}
len -= sizeof ( *fcoeftr );
fcoeftr = ( data + len );
if ( len < sizeof ( *fchdr ) ) {
logmsg ( LOG_ERR, "received truncated FCoE frame\n" );
return -1;
}
fchdr = data;
dest_id = &fchdr->d_id;
/* Intercept traffic for special port IDs */
if ( memcmp ( dest_id, &fc_gs_port_id, sizeof ( *dest_id ) ) == 0 )
return fc_gs_rx ( fchdr, len );
if ( memcmp ( dest_id, &fc_ls_port_id, sizeof ( *dest_id ) ) == 0 )
return fc_ls_rx ( fchdr, len );
/* Forward FC frame */
return fc_tx ( fchdr, len );
}