1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sd-device.h"
5 #include "sd-netlink.h"
6 
7 #include "condition.h"
8 #include "conf-parser.h"
9 #include "ethtool-util.h"
10 #include "hashmap.h"
11 #include "list.h"
12 #include "net-condition.h"
13 #include "netif-naming-scheme.h"
14 
15 typedef struct LinkConfigContext LinkConfigContext;
16 typedef struct LinkConfig LinkConfig;
17 
18 typedef enum MACAddressPolicy {
19         MAC_ADDRESS_POLICY_PERSISTENT,
20         MAC_ADDRESS_POLICY_RANDOM,
21         MAC_ADDRESS_POLICY_NONE,
22         _MAC_ADDRESS_POLICY_MAX,
23         _MAC_ADDRESS_POLICY_INVALID = -EINVAL,
24 } MACAddressPolicy;
25 
26 typedef struct Link {
27         int ifindex;
28         const char *ifname;
29         const char *new_name;
30 
31         LinkConfig *config;
32         sd_device *device;
33         sd_device_action_t action;
34 
35         char *kind;
36         char *driver;
37         uint16_t iftype;
38         uint32_t flags;
39         struct hw_addr_data hw_addr;
40         struct hw_addr_data permanent_hw_addr;
41         unsigned name_assign_type;
42         unsigned addr_assign_type;
43 } Link;
44 
45 struct LinkConfig {
46         char *filename;
47 
48         NetMatch match;
49         LIST_HEAD(Condition, conditions);
50 
51         char *description;
52         struct hw_addr_data hw_addr;
53         MACAddressPolicy mac_address_policy;
54         NamePolicy *name_policy;
55         NamePolicy *alternative_names_policy;
56         char *name;
57         char **alternative_names;
58         char *alias;
59         uint32_t txqueues;
60         uint32_t rxqueues;
61         uint32_t txqueuelen;
62         uint32_t mtu;
63         uint32_t gso_max_segments;
64         size_t gso_max_size;
65         uint64_t speed;
66         Duplex duplex;
67         int autonegotiation;
68         uint32_t advertise[N_ADVERTISE];
69         uint32_t wol;
70         char *wol_password_file;
71         uint8_t *wol_password;
72         NetDevPort port;
73         int features[_NET_DEV_FEAT_MAX];
74         netdev_channels channels;
75         netdev_ring_param ring;
76         int rx_flow_control;
77         int tx_flow_control;
78         int autoneg_flow_control;
79         netdev_coalesce_param coalesce;
80         uint8_t mdi;
81 
82         uint32_t sr_iov_num_vfs;
83         OrderedHashmap *sr_iov_by_section;
84 
85         LIST_FIELDS(LinkConfig, configs);
86 };
87 
88 int link_config_ctx_new(LinkConfigContext **ret);
89 LinkConfigContext* link_config_ctx_free(LinkConfigContext *ctx);
90 DEFINE_TRIVIAL_CLEANUP_FUNC(LinkConfigContext*, link_config_ctx_free);
91 
92 int link_load_one(LinkConfigContext *ctx, const char *filename);
93 int link_config_load(LinkConfigContext *ctx);
94 bool link_config_should_reload(LinkConfigContext *ctx);
95 
96 int link_new(LinkConfigContext *ctx, sd_netlink **rtnl, sd_device *device, Link **ret);
97 Link *link_free(Link *link);
98 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
99 
100 int link_get_config(LinkConfigContext *ctx, Link *link);
101 int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link);
102 
103 const char *mac_address_policy_to_string(MACAddressPolicy p) _const_;
104 MACAddressPolicy mac_address_policy_from_string(const char *p) _pure_;
105 
106 /* gperf lookup function */
107 const struct ConfigPerfItem* link_config_gperf_lookup(const char *key, GPERF_LEN_TYPE length);
108 
109 CONFIG_PARSER_PROTOTYPE(config_parse_ifalias);
110 CONFIG_PARSER_PROTOTYPE(config_parse_rx_tx_queues);
111 CONFIG_PARSER_PROTOTYPE(config_parse_txqueuelen);
112 CONFIG_PARSER_PROTOTYPE(config_parse_wol_password);
113 CONFIG_PARSER_PROTOTYPE(config_parse_mac_address_policy);
114 CONFIG_PARSER_PROTOTYPE(config_parse_name_policy);
115 CONFIG_PARSER_PROTOTYPE(config_parse_alternative_names_policy);
116