summaryrefslogtreecommitdiffstats
path: root/netlink/parser.h
blob: 8a4e8afa410b8fd017874d56f46f87e314e450ac (plain)
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
/*
 * parser.h - netlink command line parser
 *
 * Interface for command line parser used by netlink code.
 */

#ifndef ETHTOOL_NETLINK_PARSER_H__
#define ETHTOOL_NETLINK_PARSER_H__

#include <stddef.h>

#include "netlink.h"

/* Some commands need to generate multiple netlink messages or multiple nested
 * attributes from one set of command line parameters. Argument @group_style
 * of nl_parser() takes one of three values:
 *
 * PARSER_GROUP_NONE - no grouping, flat series of attributes (default)
 * PARSER_GROUP_NEST - one nest for each @group value (@group is nest type)
 * PARSER_GROUP_MSG  - one message for each @group value (@group is command)
 */
enum parser_group_style {
	PARSER_GROUP_NONE = 0,
	PARSER_GROUP_NEST,
	PARSER_GROUP_MSG,
};

typedef int (*param_parser_cb_t)(struct nl_context *, uint16_t, const void *,
				 struct nl_msg_buff *, void *);

struct param_parser {
	/* command line parameter handled by this entry */
	const char		*arg;
	/* group id (see enum parser_group_style above) */
	unsigned int		group;
	/* netlink attribute type */
	uint16_t		type;		/* netlink attribute type */
	/* function called to parse parameter and its value */
	param_parser_cb_t	handler;
	/* pointer passed as @data argument of the handler */
	const void		*handler_data;
	/* minimum number of extra arguments after this parameter */
	unsigned int		min_argc;
	/* if @dest is passed to nl_parser(), offset to store value */
	unsigned int		dest_offset;
	/* parameter alternative group - only one parameter from a group
	 * can be specified, 0 means no group
	 */
	unsigned int		alt_group;
};

/* data structures used for handler data */

/* used by nl_parse_lookup_u32() */
struct lookup_entry_u32 {
	const char	*arg;
	uint32_t	val;
};

/* used by nl_parse_lookup_u8() */
struct lookup_entry_u8 {
	const char	*arg;
	uint8_t		val;
};

/* used by nl_parse_byte_str() */
struct byte_str_parser_data {
	unsigned int	min_len;
	unsigned int	max_len;
	char		delim;
};

/* used for value stored by nl_parse_byte_str() */
struct byte_str_value {
	unsigned int	len;
	u8		*data;
};

/* used by nl_parse_error() */
struct error_parser_data {
	const char	*err_msg;
	int		ret_val;
	unsigned int	extra_args;
};

/* used by nl_parse_bitset() */
struct bitset_parser_data {
	bool		force_hex;
	bool		no_mask;
};

/* used by nl_parse_char_bitset() */
struct char_bitset_parser_data {
	const char	*bit_chars;
	unsigned int	nbits;
	char		reset_char;
	bool		no_mask;
};

int parse_u32(const char *arg, uint32_t *result);

/* parser handlers to use as param_parser::handler */

/* NLA_FLAG represented by on | off */
int nl_parse_flag(struct nl_context *nlctx, uint16_t type, const void *data,
		  struct nl_msg_buff *msgbuff, void *dest);
/* NLA_NUL_STRING represented by a string argument */
int nl_parse_string(struct nl_context *nlctx, uint16_t type, const void *data,
		    struct nl_msg_buff *msgbuff, void *dest);
/* NLA_U32 represented as numeric argument */
int nl_parse_direct_u32(struct nl_context *nlctx, uint16_t type,
			const void *data, struct nl_msg_buff *msgbuff,
			void *dest);
/* NLA_U8 represented as numeric argument */
int nl_parse_direct_u8(struct nl_context *nlctx, uint16_t type,
		       const void *data, struct nl_msg_buff *msgbuff,
		       void *dest);
/* NLA_U32 represented as float number of meters, converted to cm. */
int nl_parse_direct_m2cm(struct nl_context *nlctx, uint16_t type,
			 const void *data, struct nl_msg_buff *msgbuff,
			 void *dest);
/* NLA_U8 represented as on | off */
int nl_parse_u8bool(struct nl_context *nlctx, uint16_t type, const void *data,
		    struct nl_msg_buff *msgbuff, void *dest);
/* NLA_U32 represented by a string argument (lookup table) */
int nl_parse_lookup_u32(struct nl_context *nlctx, uint16_t type,
			const void *data, struct nl_msg_buff *msgbuff,
			void *dest);
/* NLA_U8 represented by a string argument (lookup table) */
int nl_parse_lookup_u8(struct nl_context *nlctx, uint16_t type,
		       const void *data, struct nl_msg_buff *msgbuff,
		       void *dest);
/* always fail (for deprecated parameters) */
int nl_parse_error(struct nl_context *nlctx, uint16_t type, const void *data,
		   struct nl_msg_buff *msgbuff, void *dest);
/* NLA_BINARY represented by %x:%x:...%x (e.g. a MAC address) */
int nl_parse_byte_str(struct nl_context *nlctx, uint16_t type,
		      const void *data, struct nl_msg_buff *msgbuff,
		      void *dest);
/* bitset represented by %x[/%x] or name on|off ... [--] */
int nl_parse_bitset(struct nl_context *nlctx, uint16_t type, const void *data,
		    struct nl_msg_buff *msgbuff, void *dest);
/* bitset represented by %u[/%u] or string (characters for bits) */
int nl_parse_char_bitset(struct nl_context *nlctx, uint16_t type,
			 const void *data, struct nl_msg_buff *msgbuff,
			 void *dest);

/* main entry point called to parse the command line */
int nl_parser(struct nl_context *nlctx, const struct param_parser *params,
	      void *dest, enum parser_group_style group_style,
	      struct nl_msg_buff **msgbuffs);

#endif /* ETHTOOL_NETLINK_PARSER_H__ */