add mirred tc action
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
This commit is contained in:
parent
d6cfd04799
commit
9073aaf67e
6 changed files with 281 additions and 0 deletions
|
@ -32,6 +32,7 @@ nobase_libnlinclude_HEADERS = \
|
|||
netlink/netlink.h \
|
||||
netlink/object.h \
|
||||
netlink/route/action.h \
|
||||
netlink/route/act/mirred.h \
|
||||
netlink/route/cls/ematch/cmp.h \
|
||||
netlink/route/cls/ematch/meta.h \
|
||||
netlink/route/cls/ematch/nbyte.h \
|
||||
|
|
32
include/linux/pkt_act.h
Normal file
32
include/linux/pkt_act.h
Normal file
|
@ -0,0 +1,32 @@
|
|||
#ifndef __LINUX_PKT_ACT_H
|
||||
#define __LINUX_PKT_ACT_H
|
||||
|
||||
#include <linux/types.h>
|
||||
#include <linux/pkt_cls.h>
|
||||
|
||||
#define TCA_ACT_MIRRED 8
|
||||
#define TCA_EGRESS_REDIR 1 /* packet redirect to EGRESS*/
|
||||
#define TCA_EGRESS_MIRROR 2 /* mirror packet to EGRESS */
|
||||
#define TCA_INGRESS_REDIR 3 /* packet redirect to INGRESS*/
|
||||
#define TCA_INGRESS_MIRROR 4 /* mirror packet to INGRESS */
|
||||
|
||||
struct tc_mirred {
|
||||
tc_gen;
|
||||
int eaction; /* one of IN/EGRESS_MIRROR/REDIR */
|
||||
__u32 ifindex; /* ifindex of egress port */
|
||||
};
|
||||
|
||||
enum {
|
||||
TCA_MIRRED_UNSPEC,
|
||||
TCA_MIRRED_TM,
|
||||
TCA_MIRRED_PARMS,
|
||||
__TCA_MIRRED_MAX
|
||||
};
|
||||
#define TCA_MIRRED_MAX (__TCA_MIRRED_MAX - 1)
|
||||
|
||||
struct rtnl_mirred
|
||||
{
|
||||
struct tc_mirred m_parm;
|
||||
};
|
||||
|
||||
#endif
|
31
include/netlink/route/act/mirred.h
Normal file
31
include/netlink/route/act/mirred.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* netlink/route/cls/mirred.h mirred action
|
||||
*
|
||||
* 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 version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* Copyright (c) 2013 Cong Wang <xiyou.wangcong@gmail.com>
|
||||
*/
|
||||
|
||||
#ifndef NETLINK_MIRRED_H_
|
||||
#define NETLINK_MIRRED_H_
|
||||
|
||||
#include <netlink/netlink.h>
|
||||
#include <netlink/cache.h>
|
||||
#include <netlink/route/action.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern int rtnl_mirred_set_action(struct rtnl_act *, int);
|
||||
extern int rtnl_mirred_set_index(struct rtnl_act *, uint32_t);
|
||||
extern int rtnl_mirred_set_policy(struct rtnl_act *, int);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
|
@ -16,6 +16,7 @@
|
|||
#include <netlink/cache.h>
|
||||
#include <netlink/route/tc.h>
|
||||
#include <netlink/utils.h>
|
||||
#include <linux/pkt_act.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
|
|
@ -68,6 +68,8 @@ libnl_route_3_la_SOURCES = \
|
|||
route/cls/fw.c route/cls/police.c route/cls/u32.c route/cls/basic.c \
|
||||
route/cls/cgroup.c \
|
||||
\
|
||||
route/act/mirred.c \
|
||||
\
|
||||
route/cls/ematch.c \
|
||||
route/cls/ematch/container.c route/cls/ematch/cmp.c \
|
||||
route/cls/ematch/nbyte.c route/cls/ematch/text.c \
|
||||
|
|
214
lib/route/act/mirred.c
Normal file
214
lib/route/act/mirred.c
Normal file
|
@ -0,0 +1,214 @@
|
|||
/*
|
||||
* lib/route/cls/mirred.c mirred action
|
||||
*
|
||||
* 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 version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* Copyright (c) 2013 Cong Wang <xiyou.wangcong@gmail.com>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup act
|
||||
* @defgroup act_mirred Mirror and Redirect
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include <netlink-private/netlink.h>
|
||||
#include <netlink-private/tc.h>
|
||||
#include <netlink/netlink.h>
|
||||
#include <netlink/attr.h>
|
||||
#include <netlink/utils.h>
|
||||
#include <netlink-private/route/tc-api.h>
|
||||
#include <netlink/route/act/mirred.h>
|
||||
|
||||
static struct nla_policy mirred_policy[TCA_MIRRED_MAX + 1] = {
|
||||
[TCA_MIRRED_PARMS] = { .minlen = sizeof(struct tc_mirred) },
|
||||
};
|
||||
|
||||
static int mirred_msg_parser(struct rtnl_tc *tc, void *data)
|
||||
{
|
||||
struct rtnl_mirred *u = data;
|
||||
struct nlattr *tb[TCA_MIRRED_MAX + 1];
|
||||
int err;
|
||||
|
||||
err = tca_parse(tb, TCA_MIRRED_MAX, tc, mirred_policy);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
if (!tb[TCA_MIRRED_PARMS])
|
||||
return -NLE_MISSING_ATTR;
|
||||
|
||||
nla_memcpy(&u->m_parm, tb[TCA_MIRRED_PARMS], sizeof(u->m_parm));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mirred_free_data(struct rtnl_tc *tc, void *data)
|
||||
{
|
||||
}
|
||||
|
||||
static int mirred_clone(void *_dst, void *_src)
|
||||
{
|
||||
struct rtnl_mirred *dst = _dst, *src = _src;
|
||||
|
||||
memcpy(&dst->m_parm, &src->m_parm, sizeof(src->m_parm));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mirred_dump_line(struct rtnl_tc *tc, void *data,
|
||||
struct nl_dump_params *p)
|
||||
{
|
||||
struct rtnl_mirred *u = data;
|
||||
if (!u)
|
||||
return;
|
||||
|
||||
nl_dump(p, " index %u", u->m_parm.ifindex);
|
||||
|
||||
if (u->m_parm.eaction == TCA_EGRESS_MIRROR)
|
||||
nl_dump(p, " egress mirror");
|
||||
else if (u->m_parm.eaction == TCA_EGRESS_REDIR)
|
||||
nl_dump(p, " egress redirect");
|
||||
|
||||
switch(u->m_parm.action) {
|
||||
case TC_ACT_UNSPEC:
|
||||
nl_dump(p, " unspecified");
|
||||
break;
|
||||
case TC_ACT_PIPE:
|
||||
nl_dump(p, " pipe");
|
||||
break;
|
||||
case TC_ACT_STOLEN:
|
||||
nl_dump(p, " stolen");
|
||||
break;
|
||||
case TC_ACT_SHOT:
|
||||
nl_dump(p, " shot");
|
||||
break;
|
||||
case TC_ACT_QUEUED:
|
||||
nl_dump(p, " queued");
|
||||
break;
|
||||
case TC_ACT_REPEAT:
|
||||
nl_dump(p, " repeat");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void mirred_dump_details(struct rtnl_tc *tc, void *data,
|
||||
struct nl_dump_params *p)
|
||||
{
|
||||
}
|
||||
|
||||
static void mirred_dump_stats(struct rtnl_tc *tc, void *data,
|
||||
struct nl_dump_params *p)
|
||||
{
|
||||
struct rtnl_mirred *u = data;
|
||||
|
||||
if (!u)
|
||||
return;
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
|
||||
static int mirred_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
|
||||
{
|
||||
struct rtnl_mirred *u = data;
|
||||
|
||||
if (!u)
|
||||
return 0;
|
||||
|
||||
NLA_PUT(msg, TCA_MIRRED_PARMS, sizeof(u->m_parm), &u->m_parm);
|
||||
return 0;
|
||||
|
||||
nla_put_failure:
|
||||
return -NLE_NOMEM;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name Attribute Modifications
|
||||
* @{
|
||||
*/
|
||||
|
||||
int rtnl_mirred_set_action(struct rtnl_act *act, int action)
|
||||
{
|
||||
struct rtnl_mirred *u;
|
||||
|
||||
if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
|
||||
return -NLE_NOMEM;
|
||||
|
||||
if (action > TCA_INGRESS_MIRROR || action < TCA_EGRESS_REDIR)
|
||||
return -NLE_INVAL;
|
||||
|
||||
switch (action) {
|
||||
case TCA_EGRESS_MIRROR:
|
||||
case TCA_EGRESS_REDIR:
|
||||
u->m_parm.eaction = action;
|
||||
break;
|
||||
case TCA_INGRESS_REDIR:
|
||||
case TCA_INGRESS_MIRROR:
|
||||
default:
|
||||
return NLE_OPNOTSUPP;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rtnl_mirred_set_index(struct rtnl_act *act, uint32_t link)
|
||||
{
|
||||
struct rtnl_mirred *u;
|
||||
|
||||
if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
|
||||
return -NLE_NOMEM;
|
||||
|
||||
u->m_parm.ifindex = link;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rtnl_mirred_set_policy(struct rtnl_act *act, int policy)
|
||||
{
|
||||
struct rtnl_mirred *u;
|
||||
|
||||
if (!(u = (struct rtnl_mirred *) rtnl_tc_data(TC_CAST(act))))
|
||||
return -NLE_NOMEM;
|
||||
|
||||
if (policy > TC_POLICE_PIPE || policy < TC_POLICE_OK)
|
||||
return -NLE_INVAL;
|
||||
|
||||
switch (u->m_parm.eaction) {
|
||||
case TCA_EGRESS_MIRROR:
|
||||
case TCA_EGRESS_REDIR:
|
||||
u->m_parm.action = policy;
|
||||
break;
|
||||
case TCA_INGRESS_REDIR:
|
||||
case TCA_INGRESS_MIRROR:
|
||||
default:
|
||||
return NLE_OPNOTSUPP;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/** @} */
|
||||
|
||||
static struct rtnl_tc_ops mirred_ops = {
|
||||
.to_kind = "mirred",
|
||||
.to_type = RTNL_TC_TYPE_ACT,
|
||||
.to_size = sizeof(struct rtnl_mirred),
|
||||
.to_msg_parser = mirred_msg_parser,
|
||||
.to_free_data = mirred_free_data,
|
||||
.to_clone = mirred_clone,
|
||||
.to_msg_fill = mirred_msg_fill,
|
||||
.to_dump = {
|
||||
[NL_DUMP_LINE] = mirred_dump_line,
|
||||
[NL_DUMP_DETAILS] = mirred_dump_details,
|
||||
[NL_DUMP_STATS] = mirred_dump_stats,
|
||||
},
|
||||
};
|
||||
|
||||
static void __init mirred_init(void)
|
||||
{
|
||||
rtnl_tc_register(&mirred_ops);
|
||||
}
|
||||
|
||||
static void __exit mirred_exit(void)
|
||||
{
|
||||
rtnl_tc_unregister(&mirred_ops);
|
||||
}
|
||||
|
||||
/** @} */
|
Loading…
Add table
Reference in a new issue