summaryrefslogtreecommitdiff
path: root/libnm-core/nm-setting-ethtool.c
blob: 7bdbcb1a22ab86fa848739e67e1af5ec4413c079 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */

/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright 2018 Red Hat, Inc.
 */

#include "nm-default.h"

#include "nm-setting-ethtool.h"

#include "nm-setting-private.h"
#include "nm-ethtool-utils.h"

/*****************************************************************************/

/**
 * SECTION:nm-setting-ethtool
 * @short_description: Describes connection properties for ethtool related options
 *
 * The #NMSettingEthtool object is a #NMSetting subclass that describes properties
 * to control network driver and hardware settings.
 **/

/*****************************************************************************/

/**
 * nm_ethtool_optname_is_feature:
 * @optname: the option name to check
 *
 * Checks whether @optname is a valid option name for an offload feature.
 *
 * %Returns: %TRUE, if @optname is valid
 *
 * Since: 1.14
 */
gboolean
nm_ethtool_optname_is_feature (const char *optname)
{
	return optname && nm_ethtool_id_is_feature (nm_ethtool_id_get_by_name (optname));
}

/*****************************************************************************/

/**
 * NMSettingEthtool:
 *
 * Ethtool Ethernet Settings
 *
 * Since: 1.14
 */
struct _NMSettingEthtool {
	NMSetting parent;
};

struct _NMSettingEthtoolClass {
	NMSettingClass parent;
};

G_DEFINE_TYPE (NMSettingEthtool, nm_setting_ethtool, NM_TYPE_SETTING)

#define NM_SETTING_ETHTOOL_GET_PRIVATE(self) _NM_GET_PRIVATE (self, NMSettingEthtool, NM_IS_SETTING_ETHTOOL, NMSetting)

/*****************************************************************************/

static void
_notify_attributes (NMSettingEthtool *self)
{
	_nm_setting_gendata_notify (NM_SETTING (self), TRUE);
}

/*****************************************************************************/

/**
 * nm_setting_ethtool_get_feature:
 * @setting: the #NMSettingEthtool
 * @optname: option name of the offload feature to get
 *
 * Gets and offload feature setting. Returns %NM_TERNARY_DEFAULT if the
 * feature is not set.
 *
 * Returns: a #NMTernary value indicating whether the offload feature
 *   is enabled, disabled, or left untouched.
 *
 * Since: 1.14
 */
NMTernary
nm_setting_ethtool_get_feature (NMSettingEthtool *setting,
                                const char *optname)
{
	GVariant *v;

	g_return_val_if_fail (NM_IS_SETTING_ETHTOOL (setting), NM_TERNARY_DEFAULT);
	g_return_val_if_fail (optname && nm_ethtool_optname_is_feature (optname), NM_TERNARY_DEFAULT);

	v = nm_setting_gendata_get (NM_SETTING (setting), optname);
	if (   v
	    && g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN)) {
		return g_variant_get_boolean (v)
		       ? NM_TERNARY_TRUE
		       : NM_TERNARY_FALSE;
	}
	return NM_TERNARY_DEFAULT;
}

/**
 * nm_setting_ethtool_set_feature:
 * @setting: the #NMSettingEthtool
 * @optname: option name of the offload feature to get
 * @value: the new value to set. The special value %NM_TERNARY_DEFAULT
 *   means to clear the offload feature setting.
 *
 * Sets and offload feature setting.
 *
 * Since: 1.14
 */
void
nm_setting_ethtool_set_feature (NMSettingEthtool *setting,
                                const char *optname,
                                NMTernary value)
{
	GHashTable *hash;
	GVariant *v;

	g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));
	g_return_if_fail (optname && nm_ethtool_optname_is_feature (optname));
	g_return_if_fail (NM_IN_SET (value, NM_TERNARY_DEFAULT,
	                                    NM_TERNARY_FALSE,
	                                    NM_TERNARY_TRUE));

	hash = _nm_setting_gendata_hash (NM_SETTING (setting),
	                                 value != NM_TERNARY_DEFAULT);

	if (value == NM_TERNARY_DEFAULT) {
		if (hash) {
			if (g_hash_table_remove (hash, optname))
				_notify_attributes (setting);
		}
		return;
	}

	v = g_hash_table_lookup (hash, optname);
	if (   v
	    && g_variant_is_of_type (v, G_VARIANT_TYPE_BOOLEAN)) {
		if (g_variant_get_boolean (v)) {
			if (value == NM_TERNARY_TRUE)
				return;
		} else {
			if (value == NM_TERNARY_FALSE)
				return;
		}
	}

	v = g_variant_ref_sink (g_variant_new_boolean (value != NM_TERNARY_FALSE));
	g_hash_table_insert (hash,
	                     g_strdup (optname),
	                     v);
	_notify_attributes (setting);
}

/**
 * nm_setting_ethtool_clear_features:
 * @setting: the #NMSettingEthtool
 *
 * Clears all offload features settings
 *
 * Since: 1.14
 */
void
nm_setting_ethtool_clear_features (NMSettingEthtool *setting)
{
	GHashTable *hash;
	GHashTableIter iter;
	const char *name;
	gboolean changed = FALSE;

	g_return_if_fail (NM_IS_SETTING_ETHTOOL (setting));

	hash = _nm_setting_gendata_hash (NM_SETTING (setting), FALSE);
	if (!hash)
		return;

	g_hash_table_iter_init (&iter, hash);
	while (g_hash_table_iter_next (&iter, (gpointer *) &name, NULL)) {
		if (nm_ethtool_optname_is_feature (name)) {
			g_hash_table_iter_remove (&iter);
			changed = TRUE;
		}
	}

	if (changed)
		_notify_attributes (setting);
}

guint
nm_setting_ethtool_init_features (NMSettingEthtool *setting,
                                  NMTernary *requested /* indexed by NMEthtoolID - _NM_ETHTOOL_ID_FEATURE_FIRST */)
{
	GHashTable *hash;
	GHashTableIter iter;
	guint i;
	guint n_req = 0;
	const char *name;
	GVariant *variant;

	nm_assert (NM_IS_SETTING_ETHTOOL (setting));
	nm_assert (requested);

	for (i = 0; i < _NM_ETHTOOL_ID_FEATURE_NUM; i++)
		requested[i] = NM_TERNARY_DEFAULT;

	hash = _nm_setting_gendata_hash (NM_SETTING (setting), FALSE);
	if (!hash)
		return 0;

	g_hash_table_iter_init (&iter, hash);
	while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &variant)) {
		NMEthtoolID ethtool_id = nm_ethtool_id_get_by_name (name);

		if (!nm_ethtool_id_is_feature (ethtool_id))
			continue;
		if (!g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN))
			continue;

		requested[ethtool_id - _NM_ETHTOOL_ID_FEATURE_FIRST] = g_variant_get_boolean (variant)
		                                                       ? NM_TERNARY_TRUE
		                                                       : NM_TERNARY_FALSE;
		n_req++;
	}

	return n_req;
}

/*****************************************************************************/

static gboolean
verify (NMSetting *setting, NMConnection *connection, GError **error)
{
	GHashTable *hash;
	GHashTableIter iter;
	const char *optname;
	GVariant *variant;

	hash = _nm_setting_gendata_hash (setting, FALSE);

	if (!hash)
		goto out;

	g_hash_table_iter_init (&iter, hash);
	while (g_hash_table_iter_next (&iter, (gpointer *) &optname, (gpointer *) &variant)) {
		if (!nm_ethtool_optname_is_feature (optname)) {
			g_set_error_literal (error,
			                     NM_CONNECTION_ERROR,
			                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
			                     _("unsupported offload feature"));
			g_prefix_error (error, "%s.%s: ", NM_SETTING_ETHTOOL_SETTING_NAME, optname);
			return FALSE;
		}
		if (!g_variant_is_of_type (variant, G_VARIANT_TYPE_BOOLEAN)) {
			g_set_error_literal (error,
			                     NM_CONNECTION_ERROR,
			                     NM_CONNECTION_ERROR_INVALID_PROPERTY,
			                     _("offload feature has invalid variant type"));
			g_prefix_error (error, "%s.%s: ", NM_SETTING_ETHTOOL_SETTING_NAME, optname);
			return FALSE;
		}
	}

out:
	return TRUE;
}

/*****************************************************************************/

static const GVariantType *
get_variant_type (const NMSettInfoSetting *sett_info,
                  const char *name,
                  GError **error)
{
	if (nm_ethtool_optname_is_feature (name))
		return G_VARIANT_TYPE_BOOLEAN;

	g_set_error (error,
	             NM_CONNECTION_ERROR,
	             NM_CONNECTION_ERROR_INVALID_PROPERTY,
	             _("unknown ethtool option '%s'"),
	             name);
	return NULL;
}

/*****************************************************************************/

static void
nm_setting_ethtool_init (NMSettingEthtool *setting)
{
}

/**
 * nm_setting_ethtool_new:
 *
 * Creates a new #NMSettingEthtool object with default values.
 *
 * Returns: (transfer full): the new empty #NMSettingEthtool object
 *
 * Since: 1.14
 **/
NMSetting *
nm_setting_ethtool_new (void)
{
	return g_object_new (NM_TYPE_SETTING_ETHTOOL, NULL);
}

static void
nm_setting_ethtool_class_init (NMSettingEthtoolClass *klass)
{
	NMSettingClass *setting_class = NM_SETTING_CLASS (klass);

	setting_class->verify = verify;

	_nm_setting_class_commit_full (setting_class,
	                               NM_META_SETTING_TYPE_ETHTOOL,
	                               NM_SETT_INFO_SETT_DETAIL (
	                                 .gendata_info = NM_SETT_INFO_SETT_GENDATA (
	                                     .get_variant_type = get_variant_type,
	                                 ),
	                               ),
	                               NULL);
}