Skip to content

Commit 0d99da8

Browse files
Moved changes to current codebase
Signed-off-by: Patryk Strusiewicz-Surmacki <[email protected]>
1 parent 2a657c2 commit 0d99da8

File tree

12 files changed

+139
-48
lines changed

12 files changed

+139
-48
lines changed

doc/.custom_wordlist.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ networkd
133133
nm
134134
passthrough
135135
programmatically
136+
pvid
136137
renderer
137138
reselection
138139
runtime
@@ -145,6 +146,8 @@ udev
145146
unconfigured
146147
unencrypted
147148
untagged
149+
vid
150+
vlans
148151
vSwitch
149152
wpa
150153
wpasupplicant

doc/netplan-yaml.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,13 @@ The specific settings for bridges are defined below.
13371337
eth1: 20
13381338
```
13391339

1340+
- **`port-vlans`** (sequence of scalars)
1341+
1342+
> Array of bridge VLAN objects. The VLAN list can be specified with the
1343+
> following syntax: $vid [pvid] [untagged] [, $vid [pvid] [untagged]]..
1344+
> where $vid is either a single id between 1 and 4094 or a range,
1345+
> represented as a couple of ids separated by a dash.
1346+
13401347
- **`forward-delay`** (scalar)
13411348

13421349
> Specify the period of time the bridge will remain in Listening and
@@ -1345,6 +1352,16 @@ The specific settings for bridges are defined below.
13451352
> If no time suffix is specified, the value will be interpreted as
13461353
> seconds.
13471354

1355+
- **`vlans`** (sequence of scalars)
1356+
> Array of bridge VLAN objects. The VLAN list can be specified with the
1357+
> following syntax: $vid [pvid] [untagged] [, $vid [pvid] [untagged]]..
1358+
> where $vid is either a single id between 1 and 4094 or a range,
1359+
> represented as a couple of ids separated by a dash.
1360+
1361+
- **`vlan-filtering`** (boolean)
1362+
1363+
> Enables VLAN filtering. Will be enabled by default if *vlans* are defined.
1364+
13481365
- **`hello-time`** (scalar)
13491366

13501367
> Specify the interval between two hello packets being sent out from

src/abi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ struct netplan_net_definition {
342342
char* max_age;
343343
guint path_cost;
344344
gboolean stp;
345+
GArray* vlans;
346+
GArray* port_vlans;
347+
gboolean vlan_filtering;
345348
} bridge_params;
346349
gboolean custom_bridging;
347350

@@ -432,3 +435,10 @@ struct netplan_net_definition {
432435

433436
NetplanRAOverrides ra_overrides;
434437
};
438+
439+
typedef struct {
440+
guint vid; //[1..4094]
441+
guint vid_to; //set iff vid range defined
442+
gboolean pvid;
443+
gboolean untagged;
444+
} NetplanBridgeVlan;

src/generate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ find_interface(gchar* interface, GHashTable* netdefs)
203203

204204
int main(int argc, char** argv)
205205
{
206+
g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, g_log_default_handler, NULL);
206207
NetplanError* error = NULL;
207208
GOptionContext* opt_context;
208209
/* are we being called as systemd generator? */

src/netplan.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "yaml-helpers.h"
2929
#include "util-internal.h"
3030
#include "names.h"
31+
#include "nm.h"
3132

3233
gchar *tmp = NULL;
3334

@@ -310,6 +311,53 @@ write_bridge_params(yaml_event_t* event, yaml_emitter_t* emitter, const NetplanN
310311
YAML_MAPPING_CLOSE(event, emitter);
311312
}
312313

314+
gboolean has_port_vlans = FALSE;
315+
for (unsigned i = 0; i < interfaces->len; ++i) {
316+
NetplanNetDefinition *nd = g_array_index(interfaces, NetplanNetDefinition*, i);
317+
if (nd->bridge_params.port_vlans || DIRTY(nd, nd->bridge_params.port_vlans)) {
318+
has_port_vlans = TRUE;
319+
break;
320+
}
321+
}
322+
323+
if (has_port_vlans) {
324+
YAML_SCALAR_PLAIN(event, emitter, "port-vlans");
325+
YAML_MAPPING_OPEN(event, emitter);
326+
for (unsigned i = 0; i < interfaces->len; ++i) {
327+
NetplanNetDefinition *nd = g_array_index(interfaces, NetplanNetDefinition*, i);
328+
if (nd->bridge_params.port_vlans || DIRTY(nd, nd->bridge_params.port_vlans)) {
329+
YAML_SCALAR_PLAIN(event, emitter, nd->id);
330+
YAML_SEQUENCE_OPEN(event, emitter);
331+
for (unsigned i = 0; i < nd->bridge_params.port_vlans->len; ++i) {
332+
NetplanBridgeVlan* vlan = g_array_index(nd->bridge_params.port_vlans, NetplanBridgeVlan*, i);
333+
GString* v = bridge_vlan_str(vlan);
334+
YAML_SCALAR_PLAIN(event, emitter, v->str);
335+
g_string_free(v, TRUE);
336+
}
337+
YAML_SEQUENCE_CLOSE(event, emitter);
338+
}
339+
340+
}
341+
YAML_MAPPING_CLOSE(event, emitter);
342+
}
343+
344+
if (def->bridge_params.vlan_filtering || def->bridge_params.vlans || DIRTY(def, def->bridge_params.vlans)) {
345+
YAML_STRING(def, event, emitter, "vlan-filtering", "true");
346+
if (def->bridge_params.vlans || DIRTY(def, def->bridge_params.vlans)) {
347+
YAML_SCALAR_PLAIN(event, emitter, "vlans");
348+
YAML_SEQUENCE_OPEN(event, emitter);
349+
for (unsigned i = 0; i < def->bridge_params.vlans->len; ++i) {
350+
NetplanBridgeVlan* vlan = g_array_index(def->bridge_params.vlans, NetplanBridgeVlan*, i);
351+
GString* v = bridge_vlan_str(vlan);
352+
YAML_SCALAR_PLAIN(event, emitter, v->str);
353+
g_string_free(v, TRUE);
354+
}
355+
YAML_SEQUENCE_CLOSE(event, emitter);
356+
}
357+
}
358+
359+
360+
313361
YAML_MAPPING_CLOSE(event, emitter);
314362
}
315363
return TRUE;

src/networkd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -987,12 +987,11 @@ _netplan_netdef_write_network_file(
987987
g_string_append_printf(network, "Learning=%s\n", def->bridge_learning ? "true" : "false");
988988
if (def->bridge_neigh_suppress != NETPLAN_TRISTATE_UNSET)
989989
g_string_append_printf(network, "NeighborSuppression=%s\n", def->bridge_neigh_suppress ? "true" : "false");
990-
if (def->bridge_params.port_vlans) {
990+
if (def->bridge_params.port_vlans) {
991991
// TODO: research and implement bridge port-vlans for networkd
992992
g_fprintf(stderr, "ERROR: %s: networkd does not support bridge port-vlans\n", def->id);
993993
exit(1);
994994
}
995-
996995
}
997996
if (def->bond && def->backend != NETPLAN_BACKEND_OVS) {
998997
g_string_append_printf(network, "Bond=%s\n", def->bond);

src/nm.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ wifi_band_str(const NetplanWifiBand band)
148148
/**
149149
* Return NM bridge vlan string.
150150
*/
151-
static const char*
151+
GString*
152152
bridge_vlan_str(const NetplanBridgeVlan* vlan)
153153
{
154154
GString* s = NULL;
155-
s = g_string_sized_new(20);
155+
s = g_string_sized_new(24);
156156

157157
g_string_append_printf(s, "%u", vlan->vid);
158158
if (vlan->vid_to)
@@ -161,7 +161,7 @@ bridge_vlan_str(const NetplanBridgeVlan* vlan)
161161
g_string_append(s, " pvid");
162162
if (vlan->untagged)
163163
g_string_append(s, " untagged");
164-
return s->str;
164+
return s;
165165
}
166166

167167
/**
@@ -395,7 +395,10 @@ write_nm_bond_parameters(const NetplanNetDefinition* def, GKeyFile *kf)
395395
STATIC void
396396
write_bridge_params_nm(const NetplanNetDefinition* def, GKeyFile *kf)
397397
{
398+
g_autoptr(GString) vlans = NULL;
399+
398400
if (def->custom_bridging) {
401+
vlans = g_string_sized_new(200);
399402
if (def->bridge_params.ageing_time)
400403
g_key_file_set_string(kf, "bridge", "ageing-time", def->bridge_params.ageing_time);
401404
if (def->bridge_params.priority)
@@ -407,17 +410,17 @@ write_bridge_params_nm(const NetplanNetDefinition* def, GKeyFile *kf)
407410
if (def->bridge_params.max_age)
408411
g_key_file_set_string(kf, "bridge", "max-age", def->bridge_params.max_age);
409412
g_key_file_set_boolean(kf, "bridge", "stp", def->bridge_params.stp);
413+
if(def->bridge_params.vlan_filtering || def->bridge_params.vlans)
414+
g_key_file_set_string(kf, "bridge", "vlan-filtering", "true");
410415
if (def->bridge_params.vlans) {
411-
g_string_append(params, "vlan-filtering=true\n");
412-
g_string_append(params, "vlans=");
413416
for (unsigned i = 0; i < def->bridge_params.vlans->len; ++i) {
414417
if (i > 0)
415-
g_string_append(params, ", ");
416-
g_string_append_printf(params, "%s", bridge_vlan_str(
417-
g_array_index(def->bridge_params.vlans,
418-
NetplanBridgeVlan*, i)));
418+
g_string_append(vlans, ", ");
419+
GString* v = bridge_vlan_str(g_array_index(def->bridge_params.vlans,NetplanBridgeVlan*, i));
420+
g_string_append_printf(vlans, "%s", v->str);
421+
g_string_free(v, TRUE);
419422
}
420-
g_string_append(params, "\n");
423+
g_key_file_set_string(kf, "bridge", "vlans", vlans->str);
421424
}
422425
}
423426
}
@@ -855,16 +858,16 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
855858
g_key_file_set_uint64(kf, "bridge-port", "priority", def->bridge_params.port_priority);
856859
if (def->bridge_hairpin != NETPLAN_TRISTATE_UNSET)
857860
g_key_file_set_boolean(kf, "bridge-port", "hairpin-mode", def->bridge_hairpin);
858-
if (def->bridge_params.port_vlans) {
859-
g_string_append(s, "vlans=");
861+
if (def->bridge_params.port_vlans) {
862+
g_autoptr(GString) vlans = g_string_sized_new(200);
860863
for (unsigned i = 0; i < def->bridge_params.port_vlans->len; ++i) {
861864
if (i > 0)
862-
g_string_append(s, ", ");
863-
g_string_append_printf(s, "%s", bridge_vlan_str(
864-
g_array_index(def->bridge_params.port_vlans,
865-
NetplanBridgeVlan*, i)));
865+
g_string_append(vlans, ", ");
866+
GString* v = bridge_vlan_str(g_array_index(def->bridge_params.port_vlans,NetplanBridgeVlan*, i));
867+
g_string_append_printf(vlans, "%s", v->str);
868+
g_string_free(v, TRUE);
866869
}
867-
g_string_append(s, "\n");
870+
g_key_file_set_string(kf, "bridge-port", "vlans", vlans->str);
868871
}
869872
}
870873
if (def->bond) {

src/nm.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ _netplan_netdef_write_nm(
2929

3030
NETPLAN_INTERNAL gboolean
3131
_netplan_nm_cleanup(const char* rootdir);
32+
33+
NETPLAN_INTERNAL GString*
34+
bridge_vlan_str(const NetplanBridgeVlan* vlan);

src/parse.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,7 +2168,7 @@ handle_bridge_port_priority(NetplanParser* npp, yaml_node_t* node, const char* k
21682168
}
21692169

21702170
static gboolean
2171-
handle_generic_vlans(yaml_document_t* doc, yaml_node_t* node, GArray** entryptr, const void* data, GError** error)
2171+
handle_generic_vlans(NetplanParser* npp, yaml_node_t* node, GArray** entryptr, GError** error)
21722172
{
21732173
static regex_t re;
21742174
static gboolean re_inited = FALSE;
@@ -2180,8 +2180,8 @@ handle_generic_vlans(yaml_document_t* doc, yaml_node_t* node, GArray** entryptr,
21802180

21812181
for (yaml_node_item_t *i = node->data.sequence.items.start; i < node->data.sequence.items.top; i++) {
21822182
g_autofree char* vlan = NULL;
2183-
yaml_node_t *entry = yaml_document_get_node(doc, *i);
2184-
assert_type(entry, YAML_SCALAR_NODE);
2183+
yaml_node_t *entry = yaml_document_get_node(&npp->doc, *i);
2184+
assert_type(npp, entry, YAML_SCALAR_NODE);
21852185

21862186
vlan = g_strdup(scalar(entry));
21872187

@@ -2191,7 +2191,7 @@ handle_generic_vlans(yaml_document_t* doc, yaml_node_t* node, GArray** entryptr,
21912191
if (regexec(&re, vlan, maxGroups, groups, 0) == 0) {
21922192
NetplanBridgeVlan* data = g_new0(NetplanBridgeVlan, 1);
21932193
for (unsigned g = 1; g < maxGroups; g = g+2) {
2194-
if (groups[g].rm_so == (size_t)-1)
2194+
if (groups[g].rm_so == (int)(size_t)-1)
21952195
continue; // Invalid group
21962196

21972197
char cursorCopy[strlen(vlan) + 1];
@@ -2200,17 +2200,17 @@ handle_generic_vlans(yaml_document_t* doc, yaml_node_t* node, GArray** entryptr,
22002200
guint v = 0;
22012201
switch (g) {
22022202
case 1:
2203-
v = g_ascii_strtoull(cursorCopy + groups[g].rm_so, NULL, 10);
2203+
v = (guint) g_ascii_strtoull(cursorCopy + groups[g].rm_so, NULL, 10);
22042204
if (v < 1 || v > 4094)
2205-
return yaml_error(node, error, "malformed vlan vid '%u', must be in range [1..4094]", v);
2205+
return yaml_error(npp, node, error, "malformed vlan vid '%u', must be in range [1..4094]", v);
22062206
data->vid = v;
22072207
break;
22082208
case 3:
2209-
v = g_ascii_strtoull(cursorCopy + groups[g].rm_so, NULL, 10);
2209+
v = (guint) g_ascii_strtoull(cursorCopy + groups[g].rm_so, NULL, 10);
22102210
if (v < 1 || v > 4094)
2211-
return yaml_error(node, error, "malformed vlan vid '%u', must be in range [1..4094]", v);
2211+
return yaml_error(npp, node, error, "malformed vlan vid '%u', must be in range [1..4094]", v);
22122212
else if (v <= data->vid)
2213-
return yaml_error(node, error, "malformed vlan vid range '%s': %u > %u!", scalar(entry), data->vid, v);
2213+
return yaml_error(npp, node, error, "malformed vlan vid range '%s': %u > %u!", scalar(entry), data->vid, v);
22142214
data->vid_to = v;
22152215
break;
22162216
case 5:
@@ -2228,41 +2228,41 @@ handle_generic_vlans(yaml_document_t* doc, yaml_node_t* node, GArray** entryptr,
22282228
continue;
22292229
}
22302230

2231-
return yaml_error(node, error, "malformed vlan '%s', must be: $vid [pvid] [untagged] [, $vid [pvid] [untagged]]", scalar(entry));
2231+
return yaml_error(npp, node, error, "malformed vlan '%s', must be: $vid [pvid] [untagged] [, $vid [pvid] [untagged]]", scalar(entry));
22322232
}
22332233

22342234
return TRUE;
22352235
}
22362236

22372237
static gboolean
2238-
handle_bridge_vlans(yaml_document_t* doc, yaml_node_t* node, const void* data, GError** error)
2238+
handle_bridge_vlans(NetplanParser* npp, yaml_node_t* node, const void *, GError** error)
22392239
{
2240-
return handle_generic_vlans(doc, node, &(cur_netdef->bridge_params.vlans), data, error);
2240+
return handle_generic_vlans(npp, node, &(npp->current.netdef->bridge_params.vlans), error);
22412241
}
22422242

22432243
static gboolean
2244-
handle_bridge_port_vlans(yaml_document_t* doc, yaml_node_t* node, const void* data, GError** error)
2244+
handle_bridge_port_vlans(NetplanParser* npp, yaml_node_t* node, const char*, const void*, GError** error)
22452245
{
22462246
for (yaml_node_pair_t* entry = node->data.mapping.pairs.start; entry < node->data.mapping.pairs.top; entry++) {
22472247
yaml_node_t* key, *value;
22482248
NetplanNetDefinition *component;
22492249
GArray** ref_ptr;
22502250

2251-
key = yaml_document_get_node(doc, entry->key);
2252-
assert_type(key, YAML_SCALAR_NODE);
2253-
value = yaml_document_get_node(doc, entry->value);
2254-
assert_type(value, YAML_SEQUENCE_NODE);
2251+
key = yaml_document_get_node(&npp->doc, entry->key);
2252+
assert_type(npp, key, YAML_SCALAR_NODE);
2253+
value = yaml_document_get_node(&npp->doc, entry->value);
2254+
assert_type(npp, value, YAML_SEQUENCE_NODE);
22552255

2256-
component = g_hash_table_lookup(netdefs, scalar(key));
2256+
component = g_hash_table_lookup(npp->parsed_defs, scalar(key));
22572257
if (!component) {
2258-
add_missing_node(key);
2258+
add_missing_node(npp, key);
22592259
} else {
22602260
ref_ptr = &(component->bridge_params.port_vlans);
22612261
if (*ref_ptr)
2262-
return yaml_error(node, error, "%s: interface '%s' already has port vlans",
2263-
cur_netdef->id, scalar(key));
2262+
return yaml_error(npp, node, error, "%s: interface '%s' already has port vlans",
2263+
npp->current.netdef->id, scalar(key));
22642264

2265-
if (!handle_generic_vlans(doc, value, ref_ptr, data, error))
2265+
if (!handle_generic_vlans(npp, value, ref_ptr, error))
22662266
return FALSE;
22672267
}
22682268
}
@@ -2277,10 +2277,11 @@ static const mapping_entry_handler bridge_params_handlers[] = {
22772277
{"max-age", YAML_SCALAR_NODE, {.generic=handle_netdef_str}, netdef_offset(bridge_params.max_age)},
22782278
{"path-cost", YAML_MAPPING_NODE, {.map={.custom=handle_bridge_path_cost}}, netdef_offset(bridge_params.path_cost)},
22792279
{"port-priority", YAML_MAPPING_NODE, {.map={.custom=handle_bridge_port_priority}}, netdef_offset(bridge_params.port_priority)},
2280-
{"port-vlans", YAML_MAPPING_NODE, handle_bridge_port_vlans},
22812280
{"priority", YAML_SCALAR_NODE, {.generic=handle_netdef_guint}, netdef_offset(bridge_params.priority)},
22822281
{"stp", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(bridge_params.stp)},
2283-
{"vlans", YAML_SEQUENCE_NODE, handle_bridge_vlans},
2282+
{"port-vlans", YAML_MAPPING_NODE, {.map={.custom=handle_bridge_port_vlans}}, netdef_offset(bridge_params.port_vlans)},
2283+
{"vlans", YAML_SEQUENCE_NODE, {.generic=handle_bridge_vlans}, netdef_offset(bridge_params.vlans)},
2284+
{"vlan-filtering", YAML_SCALAR_NODE, {.generic=handle_netdef_bool}, netdef_offset(bridge_params.vlan_filtering)},
22842285
{NULL}
22852286
};
22862287

@@ -3772,6 +3773,7 @@ netplan_parser_load_yaml(NetplanParser* npp, const char* filename, GError** erro
37723773

37733774
if (!load_yaml(filename, doc, error))
37743775
return FALSE;
3776+
37753777
return _netplan_parser_load_single_file(npp, filename, doc, error);
37763778
}
37773779

src/types.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ reset_netdef(NetplanNetDefinition* netdef, NetplanDefType new_type, NetplanBacke
339339
FREE_AND_NULLIFY(netdef->bridge_params.forward_delay);
340340
FREE_AND_NULLIFY(netdef->bridge_params.hello_time);
341341
FREE_AND_NULLIFY(netdef->bridge_params.max_age);
342+
free_garray_with_destructor(&netdef->bridge_params.vlans, g_free);
343+
free_garray_with_destructor(&netdef->bridge_params.port_vlans, g_free);
344+
netdef->bridge_params.vlan_filtering = FALSE;
342345
memset(&netdef->bridge_params, 0, sizeof(netdef->bridge_params));
343346
netdef->custom_bridging = FALSE;
344347

0 commit comments

Comments
 (0)