xref: /aosp_15_r20/external/ethtool/netlink/parser.h (revision 1b481fc3bb1b45d4cf28d1ec12969dc1055f555d)
1 /*
2  * parser.h - netlink command line parser
3  *
4  * Interface for command line parser used by netlink code.
5  */
6 
7 #ifndef ETHTOOL_NETLINK_PARSER_H__
8 #define ETHTOOL_NETLINK_PARSER_H__
9 
10 #include <stddef.h>
11 
12 #include "netlink.h"
13 
14 /* Some commands need to generate multiple netlink messages or multiple nested
15  * attributes from one set of command line parameters. Argument @group_style
16  * of nl_parser() takes one of three values:
17  *
18  * PARSER_GROUP_NONE - no grouping, flat series of attributes (default)
19  * PARSER_GROUP_NEST - one nest for each @group value (@group is nest type)
20  * PARSER_GROUP_MSG  - one message for each @group value (@group is command)
21  */
22 enum parser_group_style {
23 	PARSER_GROUP_NONE = 0,
24 	PARSER_GROUP_NEST,
25 	PARSER_GROUP_MSG,
26 };
27 
28 typedef int (*param_parser_cb_t)(struct nl_context *, uint16_t, const void *,
29 				 struct nl_msg_buff *, void *);
30 
31 struct param_parser {
32 	/* command line parameter handled by this entry */
33 	const char		*arg;
34 	/* group id (see enum parser_group_style above) */
35 	unsigned int		group;
36 	/* netlink attribute type */
37 	uint16_t		type;		/* netlink attribute type */
38 	/* function called to parse parameter and its value */
39 	param_parser_cb_t	handler;
40 	/* pointer passed as @data argument of the handler */
41 	const void		*handler_data;
42 	/* minimum number of extra arguments after this parameter */
43 	unsigned int		min_argc;
44 	/* if @dest is passed to nl_parser(), offset to store value */
45 	unsigned int		dest_offset;
46 	/* parameter alternative group - only one parameter from a group
47 	 * can be specified, 0 means no group
48 	 */
49 	unsigned int		alt_group;
50 };
51 
52 /* data structures used for handler data */
53 
54 /* used by nl_parse_lookup_u32() */
55 struct lookup_entry_u32 {
56 	const char	*arg;
57 	uint32_t	val;
58 };
59 
60 /* used by nl_parse_lookup_u8() */
61 struct lookup_entry_u8 {
62 	const char	*arg;
63 	uint8_t		val;
64 };
65 
66 /* used by nl_parse_byte_str() */
67 struct byte_str_parser_data {
68 	unsigned int	min_len;
69 	unsigned int	max_len;
70 	char		delim;
71 };
72 
73 /* used for value stored by nl_parse_byte_str() */
74 struct byte_str_value {
75 	unsigned int	len;
76 	u8		*data;
77 };
78 
79 /* used by nl_parse_error() */
80 struct error_parser_data {
81 	const char	*err_msg;
82 	int		ret_val;
83 	unsigned int	extra_args;
84 };
85 
86 /* used by nl_parse_bitset() */
87 struct bitset_parser_data {
88 	bool		force_hex;
89 	bool		no_mask;
90 };
91 
92 /* used by nl_parse_char_bitset() */
93 struct char_bitset_parser_data {
94 	const char	*bit_chars;
95 	unsigned int	nbits;
96 	char		reset_char;
97 	bool		no_mask;
98 };
99 
100 int parse_u32(const char *arg, uint32_t *result);
101 
102 /* parser handlers to use as param_parser::handler */
103 
104 /* NLA_FLAG represented by on | off */
105 int nl_parse_flag(struct nl_context *nlctx, uint16_t type, const void *data,
106 		  struct nl_msg_buff *msgbuff, void *dest);
107 /* NLA_NUL_STRING represented by a string argument */
108 int nl_parse_string(struct nl_context *nlctx, uint16_t type, const void *data,
109 		    struct nl_msg_buff *msgbuff, void *dest);
110 /* NLA_U32 represented as numeric argument */
111 int nl_parse_direct_u32(struct nl_context *nlctx, uint16_t type,
112 			const void *data, struct nl_msg_buff *msgbuff,
113 			void *dest);
114 /* NLA_U8 represented as numeric argument */
115 int nl_parse_direct_u8(struct nl_context *nlctx, uint16_t type,
116 		       const void *data, struct nl_msg_buff *msgbuff,
117 		       void *dest);
118 /* NLA_U32 represented as float number of meters, converted to cm. */
119 int nl_parse_direct_m2cm(struct nl_context *nlctx, uint16_t type,
120 			 const void *data, struct nl_msg_buff *msgbuff,
121 			 void *dest);
122 /* NLA_U8 represented as on | off */
123 int nl_parse_u8bool(struct nl_context *nlctx, uint16_t type, const void *data,
124 		    struct nl_msg_buff *msgbuff, void *dest);
125 /* NLA_U32 represented by a string argument (lookup table) */
126 int nl_parse_lookup_u32(struct nl_context *nlctx, uint16_t type,
127 			const void *data, struct nl_msg_buff *msgbuff,
128 			void *dest);
129 /* NLA_U8 represented by a string argument (lookup table) */
130 int nl_parse_lookup_u8(struct nl_context *nlctx, uint16_t type,
131 		       const void *data, struct nl_msg_buff *msgbuff,
132 		       void *dest);
133 /* always fail (for deprecated parameters) */
134 int nl_parse_error(struct nl_context *nlctx, uint16_t type, const void *data,
135 		   struct nl_msg_buff *msgbuff, void *dest);
136 /* NLA_BINARY represented by %x:%x:...%x (e.g. a MAC address) */
137 int nl_parse_byte_str(struct nl_context *nlctx, uint16_t type,
138 		      const void *data, struct nl_msg_buff *msgbuff,
139 		      void *dest);
140 /* bitset represented by %x[/%x] or name on|off ... [--] */
141 int nl_parse_bitset(struct nl_context *nlctx, uint16_t type, const void *data,
142 		    struct nl_msg_buff *msgbuff, void *dest);
143 /* bitset represented by %u[/%u] or string (characters for bits) */
144 int nl_parse_char_bitset(struct nl_context *nlctx, uint16_t type,
145 			 const void *data, struct nl_msg_buff *msgbuff,
146 			 void *dest);
147 
148 /* main entry point called to parse the command line */
149 int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
150 	      void *dest, enum parser_group_style group_style,
151 	      struct nl_msg_buff **msgbuffs);
152 
153 #endif /* ETHTOOL_NETLINK_PARSER_H__ */
154