Skip to content

Commit b151e13

Browse files
committed
[nh-encap] Add parsing support to populate a struct rtnl_nh_encap
There is support for parsing and populating rtnl_nexthop's encap. However, not for rtnl_nh's encapsulation. Refactor this and make it such that the parsing code for both is reused. Signed-off-by: Christoph Paasch <[email protected]>
1 parent c8f66f7 commit b151e13

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

lib/route/nexthop-encap.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef NETLINK_NEXTHOP_ENCAP_H_
22
#define NETLINK_NEXTHOP_ENCAP_H_
33

4+
struct rtnl_nh_encap;
5+
46
struct nh_encap_ops {
57
uint16_t encap_type;
68

79
int (*build_msg)(struct nl_msg *msg, void *priv);
8-
int (*parse_msg)(struct nlattr *nla, struct rtnl_nexthop *rtnh);
10+
int (*parse_msg)(struct nlattr *nla, struct rtnl_nh_encap **encap_out);
911

1012
int (*compare)(void *a, void *b);
1113
void *(*clone)(void *priv);
@@ -14,13 +16,11 @@ struct nh_encap_ops {
1416
void (*destructor)(void *priv);
1517
};
1618

17-
struct rtnl_nh_encap;
18-
1919
/*
2020
* generic nexthop encap
2121
*/
2222
int nh_encap_parse_msg(struct nlattr *encap, struct nlattr *encap_type,
23-
struct rtnl_nexthop *rtnh);
23+
struct rtnl_nh_encap **encap_out);
2424
int nh_encap_build_msg(struct nl_msg *msg, struct rtnl_nh_encap *rtnh_encap);
2525

2626
void nh_encap_dump(struct rtnl_nh_encap *rtnh_encap, struct nl_dump_params *dp);

lib/route/nexthop_encap.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int nh_encap_build_msg(struct nl_msg *msg, struct rtnl_nh_encap *rtnh_encap)
7272
}
7373

7474
int nh_encap_parse_msg(struct nlattr *encap, struct nlattr *encap_type,
75-
struct rtnl_nexthop *rtnh)
75+
struct rtnl_nh_encap **encap_out)
7676
{
7777
uint16_t e_type = nla_get_u16(encap_type);
7878

@@ -91,7 +91,10 @@ int nh_encap_parse_msg(struct nlattr *encap, struct nlattr *encap_type,
9191
return -NLE_MSGTYPE_NOSUPPORT;
9292
}
9393

94-
return lwtunnel_encap_types[e_type].ops->parse_msg(encap, rtnh);
94+
if (!lwtunnel_encap_types[e_type].ops->parse_msg)
95+
return -NLE_MSGTYPE_NOSUPPORT;
96+
97+
return lwtunnel_encap_types[e_type].ops->parse_msg(encap, encap_out);
9598
}
9699

97100
int nh_encap_compare(struct rtnl_nh_encap *a, struct rtnl_nh_encap *b)

lib/route/nh_encap_mpls.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ static struct nla_policy mpls_encap_policy[MPLS_IPTUNNEL_MAX + 1] = {
7070
[MPLS_IPTUNNEL_TTL] = { .type = NLA_U8 },
7171
};
7272

73-
static int mpls_encap_parse_msg(struct nlattr *nla, struct rtnl_nexthop *nh)
73+
static int mpls_encap_parse_msg(struct nlattr *nla,
74+
struct rtnl_nh_encap **encap_out)
7475
{
7576
struct nlattr *tb[MPLS_IPTUNNEL_MAX + 1];
76-
struct rtnl_nh_encap *nh_encap;
77-
struct nl_addr *labels;
77+
struct rtnl_nh_encap *nh_encap = NULL;
78+
struct nl_addr *labels = NULL;
7879
uint8_t ttl = 0;
7980
int err;
8081

@@ -99,23 +100,17 @@ static int mpls_encap_parse_msg(struct nlattr *nla, struct rtnl_nexthop *nh)
99100
}
100101

101102
err = rtnl_nh_encap_mpls(nh_encap, labels, ttl);
102-
if (err < 0) {
103-
goto err_out;
104-
}
105-
106-
err = rtnl_route_nh_set_encap(nh, nh_encap);
107-
if (err < 0) {
103+
if (err < 0)
108104
goto err_out;
109-
}
110105

106+
*encap_out = nh_encap;
111107
nl_addr_put(labels);
112108

113109
return 0;
114110

115111
err_out:
116112
rtnl_nh_encap_free(nh_encap);
117113
nl_addr_put(labels);
118-
119114
return err;
120115
}
121116

lib/route/route_obj.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,19 @@ static int parse_multipath(struct rtnl_route *route, struct nlattr *attr)
11491149
}
11501150

11511151
if (ntb[RTA_ENCAP] && ntb[RTA_ENCAP_TYPE]) {
1152+
struct rtnl_nh_encap *encap = NULL;
1153+
11521154
err = nh_encap_parse_msg(ntb[RTA_ENCAP],
11531155
ntb[RTA_ENCAP_TYPE],
1154-
nh);
1156+
&encap);
11551157
if (err < 0)
11561158
return err;
1159+
1160+
err = rtnl_route_nh_set_encap(nh, encap);
1161+
if (err < 0) {
1162+
rtnl_nh_encap_free(encap);
1163+
return err;
1164+
}
11571165
}
11581166
}
11591167

@@ -1358,13 +1366,21 @@ int rtnl_route_parse(struct nlmsghdr *nlh, struct rtnl_route **result)
13581366
}
13591367

13601368
if (tb[RTA_ENCAP] && tb[RTA_ENCAP_TYPE]) {
1369+
struct rtnl_nh_encap *encap = NULL;
1370+
13611371
if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
13621372
return -NLE_NOMEM;
13631373

13641374
err = nh_encap_parse_msg(tb[RTA_ENCAP],
1365-
tb[RTA_ENCAP_TYPE], old_nh);
1375+
tb[RTA_ENCAP_TYPE], &encap);
13661376
if (err < 0)
13671377
return err;
1378+
1379+
err = rtnl_route_nh_set_encap(old_nh, encap);
1380+
if (err < 0) {
1381+
rtnl_nh_encap_free(encap);
1382+
return err;
1383+
}
13681384
}
13691385

13701386
if (tb[RTA_NH_ID]) {

0 commit comments

Comments
 (0)