5#include <linux/lwtunnel.h>
8#include "nexthop-encap.h"
10static const struct lwtunnel_encap_type {
12 const struct nh_encap_ops *ops;
13} lwtunnel_encap_types[__LWTUNNEL_ENCAP_MAX] = {
14 [LWTUNNEL_ENCAP_NONE] = { .name =
"none" },
15 [LWTUNNEL_ENCAP_MPLS] = { .name =
"mpls", .ops = &mpls_encap_ops },
16 [LWTUNNEL_ENCAP_IP] = { .name =
"ip", .ops = &ip_encap_ops },
17 [LWTUNNEL_ENCAP_IP6] = { .name =
"ip6", .ops = &ip6_encap_ops },
18 [LWTUNNEL_ENCAP_ILA] = { .name =
"ila", .ops = &ila_encap_ops },
19 [LWTUNNEL_ENCAP_BPF] = { .name =
"bpf" },
22static const char *nh_encap_type2str(
unsigned int type)
26 if (type > LWTUNNEL_ENCAP_MAX)
29 name = lwtunnel_encap_types[type].name;
31 return name ? name :
"unknown";
40 nh_encap_type2str(rtnh_encap->ops->encap_type));
42 if (rtnh_encap->ops->dump)
43 rtnh_encap->ops->dump(rtnh_encap->priv, dp);
46int nh_encap_build_msg(
struct nl_msg *msg,
struct rtnl_nh_encap *rtnh_encap)
51 if (!rtnh_encap->ops || !rtnh_encap->ops->build_msg) {
52 NL_DBG(2,
"Nexthop encap type not implemented\n");
56 NLA_PUT_U16(msg, RTA_ENCAP_TYPE, rtnh_encap->ops->encap_type);
62 err = rtnh_encap->ops->build_msg(msg, rtnh_encap->priv);
74int nh_encap_parse_msg(
struct nlattr *encap,
struct nlattr *encap_type,
79 if (e_type == LWTUNNEL_ENCAP_NONE) {
80 NL_DBG(2,
"RTA_ENCAP_TYPE should not be LWTUNNEL_ENCAP_NONE\n");
82 goto unsupported_encap;
85 if (e_type > LWTUNNEL_ENCAP_MAX) {
86 NL_DBG(2,
"Unknown RTA_ENCAP_TYPE: %d\n", e_type);
88 goto unsupported_encap;
91 if (!lwtunnel_encap_types[e_type].ops) {
92 NL_DBG(2,
"RTA_ENCAP_TYPE %s is not implemented\n",
93 lwtunnel_encap_types[e_type].name);
95 goto unsupported_encap;
98 return lwtunnel_encap_types[e_type].ops->parse_msg(encap, encap_out);
116 if ((a && !b) || (!a && b) || (a->ops != b->ops))
119 if (!a->ops || !a->ops->compare)
122 return a->ops->compare(a->priv, b->priv);
125void *nh_encap_check_and_get_priv(
struct rtnl_nh_encap *nh_encap,
128 if (!nh_encap || !nh_encap->ops ||
129 nh_encap->ops->encap_type != encap_type) {
133 return nh_encap->priv;
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.