[ipoib] Avoid unnecessary path record lookup for broadcast address
[ipxe.git] / src / net / fcns.c
1 /*
2 * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <byteswap.h>
31 #include <ipxe/interface.h>
32 #include <ipxe/iobuf.h>
33 #include <ipxe/process.h>
34 #include <ipxe/xfer.h>
35 #include <ipxe/fc.h>
36 #include <ipxe/fcns.h>
37
38 /** @file
39 *
40 * Fibre Channel name server lookups
41 *
42 */
43
44 /** A Fibre Channel name server query */
45 struct fc_ns_query {
46 /** Reference count */
47 struct refcnt refcnt;
48 /** Fibre Channel exchange */
49 struct interface xchg;
50
51 /** Fibre Channel peer */
52 struct fc_peer *peer;
53 /** Fibre Channel port */
54 struct fc_port *port;
55
56 /** Process */
57 struct process process;
58 /** Success handler
59 *
60 * @v peer Fibre Channel peer
61 * @v port Fibre Channel port
62 * @v peer_port_id Peer port ID
63 * @ret rc Return status code
64 */
65 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
66 struct fc_port_id *peer_port_id );
67 };
68
69 /**
70 * Free name server query
71 *
72 * @v refcnt Reference count
73 */
74 static void fc_ns_query_free ( struct refcnt *refcnt ) {
75 struct fc_ns_query *query =
76 container_of ( refcnt, struct fc_ns_query, refcnt );
77
78 fc_peer_put ( query->peer );
79 fc_port_put ( query->port );
80 free ( query );
81 }
82
83 /**
84 * Close name server query
85 *
86 * @v query Name server query
87 * @v rc Reason for close
88 */
89 static void fc_ns_query_close ( struct fc_ns_query *query, int rc ) {
90
91 /* Stop process */
92 process_del ( &query->process );
93
94 /* Shut down interfaces */
95 intf_shutdown ( &query->xchg, rc );
96 }
97
98 /**
99 * Receive name server query response
100 *
101 * @v query Name server query
102 * @v iobuf I/O buffer
103 * @v meta Data transfer metadata
104 * @ret rc Return status code
105 */
106 static int fc_ns_query_deliver ( struct fc_ns_query *query,
107 struct io_buffer *iobuf,
108 struct xfer_metadata *meta __unused ) {
109 union fc_ns_response *resp = iobuf->data;
110 struct fc_port_id *peer_port_id;
111 int rc;
112
113 /* Sanity check */
114 if ( iob_len ( iobuf ) < sizeof ( resp->ct ) ) {
115 DBGC ( query, "FCNS %p received underlength response (%zd "
116 "bytes)\n", query, iob_len ( iobuf ) );
117 rc = -EINVAL;
118 goto done;
119 }
120
121 /* Handle response */
122 switch ( ntohs ( resp->ct.code ) ) {
123 case FC_GS_ACCEPT:
124 if ( iob_len ( iobuf ) < sizeof ( resp->gid_pn ) ) {
125 DBGC ( query, "FCNS %p received underlength accept "
126 "response (%zd bytes)\n",
127 query, iob_len ( iobuf ) );
128 rc = -EINVAL;
129 goto done;
130 }
131 peer_port_id = &resp->gid_pn.port_id.port_id;
132 DBGC ( query, "FCNS %p resolved %s to %s via %s\n",
133 query, fc_ntoa ( &query->peer->port_wwn ),
134 fc_id_ntoa ( peer_port_id ), query->port->name );
135 if ( ( rc = query->done ( query->peer, query->port,
136 peer_port_id ) ) != 0 )
137 goto done;
138 break;
139 case FC_GS_REJECT:
140 DBGC ( query, "FCNS %p rejected (reason %02x explanation "
141 "%02x)\n", query, resp->reject.ct.reason,
142 resp->reject.ct.explanation );
143 break;
144 default:
145 DBGC ( query, "FCNS %p received invalid response code %04x\n",
146 query, ntohs ( resp->ct.code ) );
147 rc = -ENOTSUP;
148 goto done;
149 }
150
151 rc = 0;
152 done:
153 free_iob ( iobuf );
154 fc_ns_query_close ( query, rc );
155 return rc;
156 }
157
158 /**
159 * Name server query process
160 *
161 * @v query Name server query
162 */
163 static void fc_ns_query_step ( struct fc_ns_query *query ) {
164 struct xfer_metadata meta;
165 struct fc_ns_gid_pn_request gid_pn;
166 int xchg_id;
167 int rc;
168
169 /* Create exchange */
170 if ( ( xchg_id = fc_xchg_originate ( &query->xchg, query->port,
171 &fc_gs_port_id,
172 FC_TYPE_CT ) ) < 0 ) {
173 rc = xchg_id;
174 DBGC ( query, "FCNS %p could not create exchange: %s\n",
175 query, strerror ( rc ) );
176 fc_ns_query_close ( query, rc );
177 return;
178 }
179
180 /* Construct query request */
181 memset ( &gid_pn, 0, sizeof ( gid_pn ) );
182 gid_pn.ct.revision = FC_CT_REVISION;
183 gid_pn.ct.type = FC_GS_TYPE_DS;
184 gid_pn.ct.subtype = FC_DS_SUBTYPE_NAME;
185 gid_pn.ct.code = htons ( FC_NS_GET ( FC_NS_PORT_NAME, FC_NS_PORT_ID ));
186 memcpy ( &gid_pn.port_wwn, &query->peer->port_wwn,
187 sizeof ( gid_pn.port_wwn ) );
188 memset ( &meta, 0, sizeof ( meta ) );
189 meta.flags = XFER_FL_OVER;
190
191 /* Send query */
192 if ( ( rc = xfer_deliver_raw_meta ( &query->xchg, &gid_pn,
193 sizeof ( gid_pn ), &meta ) ) != 0){
194 DBGC ( query, "FCNS %p could not deliver query: %s\n",
195 query, strerror ( rc ) );
196 fc_ns_query_close ( query, rc );
197 return;
198 }
199 }
200
201 /** Name server exchange interface operations */
202 static struct interface_operation fc_ns_query_xchg_op[] = {
203 INTF_OP ( xfer_deliver, struct fc_ns_query *, fc_ns_query_deliver ),
204 INTF_OP ( intf_close, struct fc_ns_query *, fc_ns_query_close ),
205 };
206
207 /** Name server exchange interface descriptor */
208 static struct interface_descriptor fc_ns_query_xchg_desc =
209 INTF_DESC ( struct fc_ns_query, xchg, fc_ns_query_xchg_op );
210
211 /** Name server process descriptor */
212 static struct process_descriptor fc_ns_query_process_desc =
213 PROC_DESC_ONCE ( struct fc_ns_query, process, fc_ns_query_step );
214
215 /**
216 * Issue Fibre Channel name server query
217 *
218 * @v peer Fibre Channel peer
219 * @v port Fibre Channel port
220 * @ret rc Return status code
221 */
222 int fc_ns_query ( struct fc_peer *peer, struct fc_port *port,
223 int ( * done ) ( struct fc_peer *peer, struct fc_port *port,
224 struct fc_port_id *peer_port_id ) ) {
225 struct fc_ns_query *query;
226
227 /* Allocate and initialise structure */
228 query = zalloc ( sizeof ( *query ) );
229 if ( ! query )
230 return -ENOMEM;
231 ref_init ( &query->refcnt, fc_ns_query_free );
232 intf_init ( &query->xchg, &fc_ns_query_xchg_desc, &query->refcnt );
233 process_init ( &query->process, &fc_ns_query_process_desc,
234 &query->refcnt );
235 query->peer = fc_peer_get ( peer );
236 query->port = fc_port_get ( port );
237 query->done = done;
238
239 DBGC ( query, "FCNS %p querying %s via %s\n",
240 query, fc_ntoa ( &query->peer->port_wwn ), port->name );
241
242 /* Mortalise self and return */
243 ref_put ( &query->refcnt );
244 return 0;
245 }