iPXE
fcns.c
Go to the documentation of this file.
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 =
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;
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 */
205 };
206 
207 /** Name server exchange interface descriptor */
209  INTF_DESC ( struct fc_ns_query, xchg, fc_ns_query_xchg_op );
210 
211 /** Name server process descriptor */
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 );
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 }
struct fc_ct_header ct
Common Transport header.
Definition: fcns.h:191
A process.
Definition: process.h:17
#define EINVAL
Invalid argument.
Definition: errno.h:428
An object interface operation.
Definition: interface.h:17
char name[8]
Name of this port.
Definition: fc.h:258
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
Data transfer metadata.
Definition: xfer.h:22
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:278
int fc_xchg_originate(struct interface *parent, struct fc_port *port, struct fc_port_id *peer_port_id, unsigned int type)
Originate a new Fibre Channel exchange.
Definition: fc.c:728
static void fc_ns_query_free(struct refcnt *refcnt)
Free name server query.
Definition: fcns.c:74
struct interface xchg
Fibre Channel exchange.
Definition: fcns.c:49
int xfer_deliver_raw_meta(struct interface *intf, const void *data, size_t len, struct xfer_metadata *meta)
Deliver datagram as raw data.
Definition: xfer.c:268
A Fibre Channel name server query.
Definition: fcns.c:45
const char * fc_id_ntoa(const struct fc_port_id *id)
Format Fibre Channel port ID.
Definition: fc.c:92
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
static void fc_ns_query_close(struct fc_ns_query *query, int rc)
Close name server query.
Definition: fcns.c:89
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
A Fibre Channel name server GID_PN request.
Definition: fcns.h:175
static void process_init(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process and add to process list.
Definition: process.h:161
Port name.
Definition: fcns.h:106
#define DBGC(...)
Definition: compiler.h:505
A process descriptor.
Definition: process.h:31
static void fc_port_put(struct fc_port *port)
Drop reference to Fibre Channel port.
Definition: fc.h:315
static int fc_ns_query_deliver(struct fc_ns_query *query, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
Receive name server query response.
Definition: fcns.c:106
uint8_t reason
Reason code.
Definition: fcns.h:37
#define ntohs(value)
Definition: byteswap.h:136
const char * fc_ntoa(const struct fc_name *wwn)
Format Fibre Channel WWN.
Definition: fc.c:127
#define PROC_DESC_ONCE(object_type, process, _step)
Define a process descriptor for a process that runs only once.
Definition: process.h:97
static struct process_descriptor fc_ns_query_process_desc
Name server process descriptor.
Definition: fcns.c:212
Name server.
Definition: fcns.h:88
uint16_t code
Command/response code.
Definition: fcns.h:31
A Fibre Channel port.
Definition: fc.h:252
void process_del(struct process *process)
Remove process from process list.
Definition: process.c:79
struct refcnt refcnt
Reference count.
Definition: fcns.c:47
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
Data transfer interfaces.
A reference counter.
Definition: refcnt.h:26
struct fc_ct_header ct
Common Transport header.
Definition: fcns.h:205
#define ENOMEM
Not enough space.
Definition: errno.h:534
struct fc_ns_gid_pn_response gid_pn
Get ID by port name.
Definition: fcns.h:209
void * memcpy(void *dest, const void *src, size_t len) __nonnull
u8 port
Port number.
Definition: CIB_PRM.h:31
Fibre Channel name server lookups.
struct fc_ns_reject_response reject
Rejection.
Definition: fcns.h:207
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An object interface.
Definition: interface.h:124
A Fibre Channel port identifier.
Definition: fc.h:37
Object interfaces.
uint8_t subtype
Generic service subtype.
Definition: fcns.h:25
#define FC_CT_REVISION
Fibre Channel Common Transport revision.
Definition: fcns.h:45
int(* done)(struct fc_peer *peer, struct fc_port *port, struct fc_port_id *peer_port_id)
Success handler.
Definition: fcns.c:65
int meta(WINDOW *, bool)
struct fc_port_id port_id
Port ID.
Definition: fcns.h:171
An object interface descriptor.
Definition: interface.h:55
uint8_t type
Generic service type.
Definition: fcns.h:23
Common Transport.
Definition: fc.h:194
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
Rejected.
Definition: fcns.h:58
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
static void fc_peer_put(struct fc_peer *peer)
Drop reference to Fibre Channel peer.
Definition: fc.h:390
uint8_t explanation
Reason code explanation.
Definition: fcns.h:39
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
Port ID.
Definition: fcns.h:104
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:194
Directory service.
Definition: fcns.h:50
struct fc_name port_wwn
Port name.
Definition: fc.h:347
Processes.
struct process process
Process.
Definition: fcns.c:57
Fibre Channel.
static struct fc_peer * fc_peer_get(struct fc_peer *peer)
Get reference to Fibre Channel peer.
Definition: fc.h:379
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct fc_name port_wwn
Port name.
Definition: fcns.h:179
A Fibre Channel peer.
Definition: fc.h:340
static struct fc_port * fc_port_get(struct fc_port *port)
Get reference to Fibre Channel port.
Definition: fc.h:304
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
struct fc_ct_header ct
Common Transport header.
Definition: fcns.h:177
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
void * data
Start of data.
Definition: iobuf.h:48
Get.
Definition: fcns.h:94
struct fc_ns_port_id port_id
Port ID.
Definition: fcns.h:199
static struct interface_descriptor fc_ns_query_xchg_desc
Name server exchange interface descriptor.
Definition: fcns.c:208
int fc_ns_query(struct fc_peer *peer, struct fc_port *port, int(*done)(struct fc_peer *peer, struct fc_port *port, struct fc_port_id *peer_port_id))
Issue Fibre Channel name server query.
Definition: fcns.c:222
A Fibre Channel name server response.
Definition: fcns.h:203
static void fc_ns_query_step(struct fc_ns_query *query)
Name server query process.
Definition: fcns.c:163
#define XFER_FL_OVER
Sender is relinquishing use of half-duplex channel.
Definition: xfer.h:50
struct mschapv2_challenge peer
Peer challenge.
Definition: mschapv2.h:12
Accepted.
Definition: fcns.h:56
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
struct fc_port_id fc_gs_port_id
Generic services port ID.
Definition: fc.c:71
static struct interface_operation fc_ns_query_xchg_op[]
Name server exchange interface operations.
Definition: fcns.c:202
struct fc_peer * peer
Fibre Channel peer.
Definition: fcns.c:52
String functions.
#define htons(value)
Definition: byteswap.h:135
struct bofm_section_header done
Definition: bofm_test.c:46
struct fc_port * port
Fibre Channel port.
Definition: fcns.c:54
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
uint8_t revision
Revision.
Definition: fcns.h:19
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33