iPXE
fcmgmt_cmd.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 <stdio.h>
27 #include <errno.h>
28 #include <getopt.h>
29 #include <strings.h>
30 #include <ipxe/fc.h>
31 #include <ipxe/fcels.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <ipxe/tables.h>
35 #include <usr/fcmgmt.h>
36 
37 /** @file
38  *
39  * Fibre Channel management commands
40  *
41  */
42 
43 /**
44  * Parse Fibre Channel port name
45  *
46  * @v text Text
47  * @ret port Fibre Channel port
48  * @ret rc Return status code
49  */
50 static int parse_fc_port ( char *text, struct fc_port **port ) {
51 
52  /* Sanity check */
53  assert ( text != NULL );
54 
55  /* Find Fibre Channel port */
56  *port = fc_port_find ( text );
57  if ( ! *port ) {
58  printf ( "\"%s\": no such port\n", text );
59  return -ENODEV;
60  }
61 
62  return 0;
63 }
64 
65 /**
66  * Parse Fibre Channel port ID
67  *
68  * @v text Text
69  * @ret port_id Fibre Channel port ID
70  * @ret rc Return status code
71  */
72 static int parse_fc_port_id ( char *text, struct fc_port_id *port_id ) {
73  int rc;
74 
75  /* Sanity check */
76  assert ( text != NULL );
77 
78  /* Parse port ID */
79  if ( ( rc = fc_id_aton ( text, port_id ) ) != 0 ) {
80  printf ( "\"%s\": invalid port ID\n", text );
81  return -EINVAL;
82  }
83 
84  return 0;
85 }
86 
87 /**
88  * Parse Fibre Channel ELS handler name
89  *
90  * @v text Text
91  * @ret handler Fibre Channel ELS handler
92  * @ret rc Return status code
93  */
94 static int parse_fc_els_handler ( char *text, struct fc_els_handler **handler ){
95 
96  for_each_table_entry ( (*handler), FC_ELS_HANDLERS ) {
97  if ( strcasecmp ( (*handler)->name, text ) == 0 )
98  return 0;
99  }
100 
101  printf ( "\"%s\": unrecognised ELS\n", text );
102  return -ENOENT;
103 }
104 
105 /** "fcstat" options */
106 struct fcstat_options {};
107 
108 /** "fcstat" option list */
109 static struct option_descriptor fcstat_opts[] = {};
110 
111 /** "fcstat" command descriptor */
113  COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0, NULL );
114 
115 /**
116  * The "fcstat" command
117  *
118  * @v argc Argument count
119  * @v argv Argument list
120  * @ret rc Return status code
121  */
122 static int fcstat_exec ( int argc, char **argv ) {
123  struct fcstat_options opts;
124  struct fc_port *port;
125  struct fc_peer *peer;
126  int rc;
127 
128  /* Parse options */
129  if ( ( rc = parse_options ( argc, argv, &fcstat_cmd, &opts ) ) != 0 )
130  return rc;
131 
133  fcportstat ( port );
135  fcpeerstat ( peer );
136 
137  return 0;
138 }
139 
140 /** "fcels" options */
142  /** Fibre Channel port */
143  struct fc_port *port;
144  /** Fibre Channel peer port ID */
146 };
147 
148 /** "fcels" option list */
149 static struct option_descriptor fcels_opts[] = {
150  OPTION_DESC ( "port", 'p', required_argument,
151  struct fcels_options, port, parse_fc_port ),
152  OPTION_DESC ( "id", 'i', required_argument,
153  struct fcels_options, peer_port_id, parse_fc_port_id ),
154 };
155 
156 /** "fcels" command descriptor */
158  COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1, "<request>" );
159 
160 /**
161  * The "fcels" command
162  *
163  * @v argc Argument count
164  * @v argv Argument list
165  * @ret rc Return status code
166  */
167 static int fcels_exec ( int argc, char **argv ) {
168  struct fcels_options opts;
169  struct fc_els_handler *handler;
170  struct fc_port_id *id;
171  int rc;
172 
173  /* Parse options */
174  if ( ( rc = parse_options ( argc, argv, &fcels_cmd, &opts ) ) != 0 )
175  return rc;
176 
177  /* Parse ELS handler */
178  if ( ( rc = parse_fc_els_handler ( argv[optind], &handler ) ) != 0 )
179  return rc;
180 
181  /* Use first port if no port specified */
182  if ( ! opts.port ) {
183  opts.port = list_first_entry ( &fc_ports, struct fc_port,
184  list );
185  if ( ! opts.port ) {
186  printf ( "No ports\n" );
187  return -ENODEV;
188  }
189  }
190 
191  /* Use link peer port ID if no peer port ID specified */
192  id = &opts.peer_port_id;
193  if ( memcmp ( id, &fc_empty_port_id, sizeof ( *id ) ) == 0 ) {
194  if ( fc_link_ok ( &opts.port->link ) &&
195  ! ( opts.port->flags & FC_PORT_HAS_FABRIC ) ) {
196  id = &opts.port->ptp_link_port_id;
197  } else {
198  id = &fc_f_port_id;
199  }
200  }
201 
202  /** Issue ELS */
203  if ( ( rc = fcels ( opts.port, id, handler ) ) != 0 )
204  return rc;
205 
206  return 0;
207 }
208 
209 /** Fibre Channel management commands */
210 struct command fcmgmt_commands[] __command = {
211  {
212  .name = "fcstat",
213  .exec = fcstat_exec,
214  },
215  {
216  .name = "fcels",
217  .exec = fcels_exec,
218  },
219 };
#define EINVAL
Invalid argument.
Definition: errno.h:428
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct list_head fc_peers
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
int fcels(struct fc_port *port, struct fc_port_id *peer_port_id, struct fc_els_handler *handler)
Issue Fibre Channel ELS.
Definition: fcmgmt.c:105
int optind
Current option index.
Definition: getopt.c:51
static struct command_descriptor fcels_cmd
"fcels" command descriptor
Definition: fcmgmt_cmd.c:157
Error codes.
A Fibre Channel extended link services handler.
Definition: fcels.h:352
A command-line command.
Definition: command.h:9
static int fcstat_exec(int argc, char **argv)
The "fcstat" command.
Definition: fcmgmt_cmd.c:122
static int parse_fc_els_handler(char *text, struct fc_els_handler **handler)
Parse Fibre Channel ELS handler name.
Definition: fcmgmt_cmd.c:94
struct fc_port_id fc_f_port_id
F_Port contoller port ID.
Definition: fc.c:68
#define ENOENT
No such file or directory.
Definition: errno.h:514
int strcasecmp(const char *first, const char *second)
Compare case-insensitive strings.
Definition: string.c:208
int parse_options(int argc, char **argv, struct command_descriptor *cmd, void *opts)
Parse command-line options.
Definition: parseopt.c:484
static int fcels_exec(int argc, char **argv)
The "fcels" command.
Definition: fcmgmt_cmd.c:167
A command descriptor.
Definition: parseopt.h:77
struct list_head fc_ports
A Fibre Channel port.
Definition: fc.h:252
void fcportstat(struct fc_port *port)
Print status of Fibre Channel port.
Definition: fcmgmt.c:45
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
u8 port
Port number.
Definition: CIB_PRM.h:31
Fibre Channel management.
Port is attached to a fabric.
Definition: fc.h:292
Parse command-line options.
struct fc_port_id peer_port_id
Fibre Channel peer port ID.
Definition: fcmgmt_cmd.c:145
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A Fibre Channel port identifier.
Definition: fc.h:37
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
static int fc_link_ok(struct fc_link_state *link)
Check Fibre Channel link state.
Definition: fc.h:108
void fcpeerstat(struct fc_peer *peer)
Print status of Fibre Channel peer.
Definition: fcmgmt.c:68
struct command fcmgmt_commands [] __command
Fibre Channel management commands.
Definition: fcmgmt_cmd.c:210
static struct command_descriptor fcstat_cmd
"fcstat" command descriptor
Definition: fcmgmt_cmd.c:112
struct list_head list
List of all peers.
Definition: fc.h:344
static struct option_descriptor fcels_opts[]
"fcels" option list
Definition: fcmgmt_cmd.c:149
struct fc_port_id fc_empty_port_id
Unassigned port ID.
Definition: fc.c:65
uint8_t id
Request identifier.
Definition: ena.h:12
"fcstat" options
Definition: fcmgmt_cmd.c:106
static int parse_fc_port_id(char *text, struct fc_port_id *port_id)
Parse Fibre Channel port ID.
Definition: fcmgmt_cmd.c:72
Command line option parsing.
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
#define ENODEV
No such device.
Definition: errno.h:509
static struct option_descriptor fcstat_opts[]
"fcstat" option list
Definition: fcmgmt_cmd.c:109
Fibre Channel.
static union @437 opts
"cert<xxx>" option list
#define FC_ELS_HANDLERS
Fibre Channel ELS handler table.
Definition: fcels.h:380
A Fibre Channel peer.
Definition: fc.h:340
const char * name
Name of the command.
Definition: command.h:11
struct fc_port * port
Fibre Channel port.
Definition: fcmgmt_cmd.c:143
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
"fcels" options
Definition: fcmgmt_cmd.c:141
#define OPTION_DESC(_longopt, _shortopt, _has_arg, _struct, _field, _parse)
Construct option descriptor.
Definition: parseopt.h:67
Option requires an argument.
Definition: getopt.h:18
A command-line option descriptor.
Definition: parseopt.h:23
static int parse_fc_port(char *text, struct fc_port **port)
Parse Fibre Channel port name.
Definition: fcmgmt_cmd.c:50
struct fc_port * fc_port_find(const char *name)
Find Fibre Channel port by name.
Definition: fc.c:1224
Linker tables.
#define COMMAND_DESC(_struct, _options, _min_args, _max_args, _usage)
Construct command descriptor.
Definition: parseopt.h:108
struct mschapv2_challenge peer
Peer challenge.
Definition: mschapv2.h:12
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
Fibre Channel Extended Link Services.
int fc_id_aton(const char *id_text, struct fc_port_id *id)
Parse Fibre Channel port ID.
Definition: fc.c:107