2002-10-31 23:03:09 +00:00
|
|
|
/*
|
|
|
|
* ng_source.c
|
2005-01-07 01:45:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*-
|
2005-04-05 17:22:05 +00:00
|
|
|
* Copyright (c) 2005 Gleb Smirnoff <glebius@FreeBSD.org>
|
2002-10-31 23:03:09 +00:00
|
|
|
* Copyright 2002 Sandvine Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
2002-11-02 01:26:28 +00:00
|
|
|
* without modifications are expressly permitted by Sandvine Inc.; provided,
|
2002-10-31 23:03:09 +00:00
|
|
|
* however, that:
|
2002-11-02 01:26:28 +00:00
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
2002-10-31 23:03:09 +00:00
|
|
|
* 2. No rights are granted, in any manner or form, to use Sandvine Inc.
|
2002-11-02 01:26:28 +00:00
|
|
|
* trademarks, including the mark "SANDVINE" on advertising, endorsements,
|
|
|
|
* or otherwise except as such appears in the above copyright notice or in
|
2002-10-31 23:03:09 +00:00
|
|
|
* the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY SANDVINE "AS IS", AND TO THE MAXIMUM
|
2002-11-02 01:26:28 +00:00
|
|
|
* EXTENT PERMITTED BY LAW, SANDVINE MAKES NO REPRESENTATIONS OR WARRANTIES,
|
|
|
|
* EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE, INCLUDING WITHOUT LIMITATION,
|
|
|
|
* ANY AND ALL IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
2002-10-31 23:03:09 +00:00
|
|
|
* PURPOSE, OR NON-INFRINGEMENT. SANDVINE DOES NOT WARRANT, GUARANTEE, OR
|
|
|
|
* MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE
|
|
|
|
* USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY
|
|
|
|
* OR OTHERWISE. IN NO EVENT SHALL SANDVINE BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
2002-11-02 01:26:28 +00:00
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
2002-10-31 23:03:09 +00:00
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF SANDVINE IS ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
2010-07-06 16:42:11 +00:00
|
|
|
* Author: Dave Chapeskie
|
2002-10-31 23:03:09 +00:00
|
|
|
*/
|
|
|
|
|
2004-01-27 10:45:37 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
/*
|
|
|
|
* This node is used for high speed packet geneneration. It queues
|
2005-03-10 21:50:50 +00:00
|
|
|
* all data recieved on its 'input' hook and when told to start via
|
|
|
|
* a control message it sends the packets out its 'output' hook. In
|
|
|
|
* this way this node can be preloaded with a packet stream which it
|
|
|
|
* can then send continuously as fast as possible.
|
2002-10-31 23:03:09 +00:00
|
|
|
*
|
|
|
|
* Currently it just copies the mbufs as required. It could do various
|
|
|
|
* tricks to try and avoid this. Probably the best performance would
|
|
|
|
* be achieved by modifying the appropriate drivers to be told to
|
|
|
|
* self-re-enqueue packets (e.g. the if_bge driver could reuse the same
|
|
|
|
* transmit descriptors) under control of this node; perhaps via some
|
2005-03-10 21:50:50 +00:00
|
|
|
* flag in the mbuf or some such. The node could peek at an appropriate
|
2002-10-31 23:03:09 +00:00
|
|
|
* ifnet flag to see if such support is available for the connected
|
|
|
|
* interface.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
2005-04-05 17:22:05 +00:00
|
|
|
#include <sys/syslog.h>
|
2002-10-31 23:03:09 +00:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_var.h>
|
|
|
|
#include <netgraph/ng_message.h>
|
|
|
|
#include <netgraph/netgraph.h>
|
|
|
|
#include <netgraph/ng_parse.h>
|
|
|
|
#include <netgraph/ng_ether.h>
|
|
|
|
#include <netgraph/ng_source.h>
|
|
|
|
|
|
|
|
#define NG_SOURCE_INTR_TICKS 1
|
|
|
|
#define NG_SOURCE_DRIVER_IFQ_MAXLEN (4*1024)
|
|
|
|
|
2007-03-01 23:16:17 +00:00
|
|
|
#define mtod_off(m,off,t) ((t)(mtod((m),caddr_t)+(off)))
|
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
/* Per node info */
|
|
|
|
struct privdata {
|
|
|
|
node_p node;
|
2005-04-05 17:22:05 +00:00
|
|
|
hook_p input;
|
|
|
|
hook_p output;
|
2002-10-31 23:03:09 +00:00
|
|
|
struct ng_source_stats stats;
|
2002-11-02 01:26:28 +00:00
|
|
|
struct ifqueue snd_queue; /* packets to send */
|
2007-03-02 01:44:04 +00:00
|
|
|
struct mbuf *last_packet; /* last pkt in queue */
|
2002-10-31 23:03:09 +00:00
|
|
|
struct ifnet *output_ifp;
|
2004-11-02 21:24:30 +00:00
|
|
|
struct callout intr_ch;
|
2005-04-05 17:22:05 +00:00
|
|
|
uint64_t packets; /* packets to send */
|
|
|
|
uint32_t queueOctets;
|
2007-03-01 23:16:17 +00:00
|
|
|
struct ng_source_embed_info embed_timestamp;
|
2007-03-02 01:44:04 +00:00
|
|
|
struct ng_source_embed_cnt_info embed_counter[NG_SOURCE_COUNTERS];
|
2002-10-31 23:03:09 +00:00
|
|
|
};
|
|
|
|
typedef struct privdata *sc_p;
|
|
|
|
|
|
|
|
/* Node flags */
|
|
|
|
#define NG_SOURCE_ACTIVE (NGF_TYPE1)
|
|
|
|
|
|
|
|
/* Netgraph methods */
|
|
|
|
static ng_constructor_t ng_source_constructor;
|
|
|
|
static ng_rcvmsg_t ng_source_rcvmsg;
|
|
|
|
static ng_shutdown_t ng_source_rmnode;
|
|
|
|
static ng_newhook_t ng_source_newhook;
|
2005-04-05 17:22:05 +00:00
|
|
|
static ng_connect_t ng_source_connect;
|
2002-10-31 23:03:09 +00:00
|
|
|
static ng_rcvdata_t ng_source_rcvdata;
|
|
|
|
static ng_disconnect_t ng_source_disconnect;
|
|
|
|
|
|
|
|
/* Other functions */
|
2004-01-30 15:34:57 +00:00
|
|
|
static void ng_source_intr(node_p, hook_p, void *, int);
|
2002-10-31 23:03:09 +00:00
|
|
|
static void ng_source_clr_data (sc_p);
|
2005-04-05 17:22:05 +00:00
|
|
|
static int ng_source_start (sc_p, uint64_t);
|
2002-10-31 23:03:09 +00:00
|
|
|
static void ng_source_stop (sc_p);
|
|
|
|
static int ng_source_send (sc_p, int, int *);
|
2005-04-05 17:22:05 +00:00
|
|
|
static int ng_source_store_output_ifp(sc_p, char *);
|
2007-03-01 23:16:17 +00:00
|
|
|
static void ng_source_packet_mod(sc_p, struct mbuf *,
|
|
|
|
int, int, caddr_t, int);
|
2007-03-02 01:44:04 +00:00
|
|
|
static void ng_source_mod_counter(sc_p sc,
|
|
|
|
struct ng_source_embed_cnt_info *cnt,
|
|
|
|
struct mbuf *m, int increment);
|
2007-03-01 23:16:17 +00:00
|
|
|
static int ng_source_dup_mod(sc_p, struct mbuf *,
|
|
|
|
struct mbuf **);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
|
|
|
/* Parse type for timeval */
|
2004-01-27 10:45:37 +00:00
|
|
|
static const struct ng_parse_struct_field ng_source_timeval_type_fields[] = {
|
2002-10-31 23:03:09 +00:00
|
|
|
{ "tv_sec", &ng_parse_int32_type },
|
|
|
|
{ "tv_usec", &ng_parse_int32_type },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
const struct ng_parse_type ng_source_timeval_type = {
|
|
|
|
&ng_parse_struct_type,
|
|
|
|
&ng_source_timeval_type_fields
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Parse type for struct ng_source_stats */
|
|
|
|
static const struct ng_parse_struct_field ng_source_stats_type_fields[]
|
|
|
|
= NG_SOURCE_STATS_TYPE_INFO;
|
|
|
|
static const struct ng_parse_type ng_source_stats_type = {
|
|
|
|
&ng_parse_struct_type,
|
|
|
|
&ng_source_stats_type_fields
|
|
|
|
};
|
|
|
|
|
2007-03-01 23:16:17 +00:00
|
|
|
/* Parse type for struct ng_source_embed_info */
|
|
|
|
static const struct ng_parse_struct_field ng_source_embed_type_fields[] =
|
|
|
|
NG_SOURCE_EMBED_TYPE_INFO;
|
|
|
|
static const struct ng_parse_type ng_source_embed_type = {
|
|
|
|
&ng_parse_struct_type,
|
|
|
|
&ng_source_embed_type_fields
|
|
|
|
};
|
|
|
|
|
2007-03-02 01:44:04 +00:00
|
|
|
/* Parse type for struct ng_source_embed_cnt_info */
|
|
|
|
static const struct ng_parse_struct_field ng_source_embed_cnt_type_fields[] =
|
|
|
|
NG_SOURCE_EMBED_CNT_TYPE_INFO;
|
|
|
|
static const struct ng_parse_type ng_source_embed_cnt_type = {
|
|
|
|
&ng_parse_struct_type,
|
|
|
|
&ng_source_embed_cnt_type_fields
|
|
|
|
};
|
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
/* List of commands and how to convert arguments to/from ASCII */
|
|
|
|
static const struct ng_cmdlist ng_source_cmds[] = {
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_GET_STATS,
|
|
|
|
"getstats",
|
|
|
|
NULL,
|
|
|
|
&ng_source_stats_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_CLR_STATS,
|
|
|
|
"clrstats",
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_GETCLR_STATS,
|
|
|
|
"getclrstats",
|
|
|
|
NULL,
|
|
|
|
&ng_source_stats_type
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_START,
|
|
|
|
"start",
|
|
|
|
&ng_parse_uint64_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_STOP,
|
|
|
|
"stop",
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_CLR_DATA,
|
|
|
|
"clrdata",
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
},
|
2004-01-26 14:53:16 +00:00
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
2005-04-05 17:22:05 +00:00
|
|
|
NGM_SOURCE_SETIFACE,
|
|
|
|
"setiface",
|
|
|
|
&ng_parse_string_type,
|
2004-01-26 14:53:16 +00:00
|
|
|
NULL
|
|
|
|
},
|
2005-12-23 19:14:38 +00:00
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_SETPPS,
|
|
|
|
"setpps",
|
|
|
|
&ng_parse_uint32_type,
|
|
|
|
NULL
|
|
|
|
},
|
2007-03-01 23:16:17 +00:00
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_SET_TIMESTAMP,
|
|
|
|
"settimestamp",
|
|
|
|
&ng_source_embed_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_GET_TIMESTAMP,
|
|
|
|
"gettimestamp",
|
|
|
|
NULL,
|
|
|
|
&ng_source_embed_type
|
|
|
|
},
|
2007-03-02 01:44:04 +00:00
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_SET_COUNTER,
|
|
|
|
"setcounter",
|
|
|
|
&ng_source_embed_cnt_type,
|
|
|
|
NULL
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NGM_SOURCE_COOKIE,
|
|
|
|
NGM_SOURCE_GET_COUNTER,
|
|
|
|
"getcounter",
|
|
|
|
&ng_parse_uint8_type,
|
|
|
|
&ng_source_embed_cnt_type
|
|
|
|
},
|
2002-10-31 23:03:09 +00:00
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Netgraph type descriptor */
|
|
|
|
static struct ng_type ng_source_typestruct = {
|
2004-05-29 00:51:19 +00:00
|
|
|
.version = NG_ABI_VERSION,
|
|
|
|
.name = NG_SOURCE_NODE_TYPE,
|
|
|
|
.constructor = ng_source_constructor,
|
|
|
|
.rcvmsg = ng_source_rcvmsg,
|
|
|
|
.shutdown = ng_source_rmnode,
|
|
|
|
.newhook = ng_source_newhook,
|
2005-04-05 17:22:05 +00:00
|
|
|
.connect = ng_source_connect,
|
2004-05-29 00:51:19 +00:00
|
|
|
.rcvdata = ng_source_rcvdata,
|
|
|
|
.disconnect = ng_source_disconnect,
|
|
|
|
.cmdlist = ng_source_cmds,
|
2002-10-31 23:03:09 +00:00
|
|
|
};
|
|
|
|
NETGRAPH_INIT(source, &ng_source_typestruct);
|
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
static int ng_source_set_autosrc(sc_p, uint32_t);
|
2004-01-26 14:48:21 +00:00
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
/*
|
|
|
|
* Node constructor
|
|
|
|
*/
|
|
|
|
static int
|
2002-11-02 02:29:43 +00:00
|
|
|
ng_source_constructor(node_p node)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
|
|
|
sc_p sc;
|
|
|
|
|
2011-04-18 09:12:27 +00:00
|
|
|
sc = malloc(sizeof(*sc), M_NETGRAPH, M_WAITOK | M_ZERO);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2002-11-02 02:29:43 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, sc);
|
|
|
|
sc->node = node;
|
2002-10-31 23:03:09 +00:00
|
|
|
sc->snd_queue.ifq_maxlen = 2048; /* XXX not checked */
|
2004-11-02 21:24:30 +00:00
|
|
|
ng_callout_init(&sc->intr_ch);
|
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a hook
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_source_newhook(node_p node, hook_p hook, const char *name)
|
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
sc_p sc = NG_NODE_PRIVATE(node);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
|
|
|
if (strcmp(name, NG_SOURCE_HOOK_INPUT) == 0) {
|
2005-04-05 17:22:05 +00:00
|
|
|
sc->input = hook;
|
2002-10-31 23:03:09 +00:00
|
|
|
} else if (strcmp(name, NG_SOURCE_HOOK_OUTPUT) == 0) {
|
2005-04-05 17:22:05 +00:00
|
|
|
sc->output = hook;
|
2002-10-31 23:03:09 +00:00
|
|
|
sc->output_ifp = 0;
|
|
|
|
bzero(&sc->stats, sizeof(sc->stats));
|
|
|
|
} else
|
|
|
|
return (EINVAL);
|
2005-04-05 17:22:05 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook has been added
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_source_connect(hook_p hook)
|
|
|
|
{
|
|
|
|
sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
|
|
|
struct ng_mesg *msg;
|
|
|
|
int dummy_error = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is "output" hook, then request information
|
|
|
|
* from our downstream.
|
|
|
|
*/
|
|
|
|
if (hook == sc->output) {
|
|
|
|
NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_GET_IFNAME,
|
|
|
|
0, M_NOWAIT);
|
|
|
|
if (msg == NULL)
|
|
|
|
return (ENOBUFS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Our hook and peer hook have HK_INVALID flag set,
|
|
|
|
* so we can't use NG_SEND_MSG_HOOK() macro here.
|
|
|
|
*/
|
|
|
|
NG_SEND_MSG_ID(dummy_error, sc->node, msg,
|
|
|
|
NG_NODE_ID(NG_PEER_NODE(sc->output)), NG_NODE_ID(sc->node));
|
|
|
|
}
|
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive a control message
|
|
|
|
*/
|
|
|
|
static int
|
2002-11-02 02:29:43 +00:00
|
|
|
ng_source_rcvmsg(node_p node, item_p item, hook_p lasthook)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
sc_p sc = NG_NODE_PRIVATE(node);
|
|
|
|
struct ng_mesg *msg, *resp = NULL;
|
2002-10-31 23:03:09 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2002-11-02 02:29:43 +00:00
|
|
|
NGI_GET_MSG(item, msg);
|
2005-04-05 17:22:05 +00:00
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
switch (msg->header.typecookie) {
|
|
|
|
case NGM_SOURCE_COOKIE:
|
2002-11-05 01:08:11 +00:00
|
|
|
if (msg->header.flags & NGF_RESP) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2002-10-31 23:03:09 +00:00
|
|
|
switch (msg->header.cmd) {
|
|
|
|
case NGM_SOURCE_GET_STATS:
|
|
|
|
case NGM_SOURCE_CLR_STATS:
|
|
|
|
case NGM_SOURCE_GETCLR_STATS:
|
|
|
|
{
|
|
|
|
struct ng_source_stats *stats;
|
|
|
|
|
|
|
|
if (msg->header.cmd != NGM_SOURCE_CLR_STATS) {
|
|
|
|
NG_MKRESPONSE(resp, msg,
|
|
|
|
sizeof(*stats), M_NOWAIT);
|
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
sc->stats.queueOctets = sc->queueOctets;
|
2002-11-02 01:26:28 +00:00
|
|
|
sc->stats.queueFrames = sc->snd_queue.ifq_len;
|
2002-11-02 02:29:43 +00:00
|
|
|
if ((sc->node->nd_flags & NG_SOURCE_ACTIVE)
|
2002-10-31 23:03:09 +00:00
|
|
|
&& !timevalisset(&sc->stats.endTime)) {
|
2002-11-02 01:26:28 +00:00
|
|
|
getmicrotime(&sc->stats.elapsedTime);
|
2002-10-31 23:03:09 +00:00
|
|
|
timevalsub(&sc->stats.elapsedTime,
|
2002-11-02 01:26:28 +00:00
|
|
|
&sc->stats.startTime);
|
2002-10-31 23:03:09 +00:00
|
|
|
}
|
2002-11-02 01:26:28 +00:00
|
|
|
stats = (struct ng_source_stats *)resp->data;
|
2002-10-31 23:03:09 +00:00
|
|
|
bcopy(&sc->stats, stats, sizeof(* stats));
|
|
|
|
}
|
|
|
|
if (msg->header.cmd != NGM_SOURCE_GET_STATS)
|
|
|
|
bzero(&sc->stats, sizeof(sc->stats));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NGM_SOURCE_START:
|
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
uint64_t packets;
|
|
|
|
|
|
|
|
if (msg->header.arglen != sizeof(uint64_t)) {
|
2004-01-26 14:53:16 +00:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2005-04-05 17:22:05 +00:00
|
|
|
|
|
|
|
packets = *(uint64_t *)msg->data;
|
|
|
|
|
|
|
|
error = ng_source_start(sc, packets);
|
|
|
|
|
|
|
|
break;
|
2004-01-26 14:53:16 +00:00
|
|
|
}
|
2002-10-31 23:03:09 +00:00
|
|
|
case NGM_SOURCE_STOP:
|
|
|
|
ng_source_stop(sc);
|
|
|
|
break;
|
|
|
|
case NGM_SOURCE_CLR_DATA:
|
|
|
|
ng_source_clr_data(sc);
|
|
|
|
break;
|
2005-04-05 17:22:05 +00:00
|
|
|
case NGM_SOURCE_SETIFACE:
|
|
|
|
{
|
|
|
|
char *ifname = (char *)msg->data;
|
|
|
|
|
|
|
|
if (msg->header.arglen < 2) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ng_source_store_output_ifp(sc, ifname);
|
2005-12-23 19:14:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_SOURCE_SETPPS:
|
|
|
|
{
|
|
|
|
uint32_t pps;
|
|
|
|
|
|
|
|
if (msg->header.arglen != sizeof(uint32_t)) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pps = *(uint32_t *)msg->data;
|
|
|
|
|
|
|
|
sc->stats.maxPps = pps;
|
|
|
|
|
2007-03-01 23:16:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_SOURCE_SET_TIMESTAMP:
|
|
|
|
{
|
|
|
|
struct ng_source_embed_info *embed;
|
|
|
|
|
2007-03-02 14:36:19 +00:00
|
|
|
if (msg->header.arglen != sizeof(*embed)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-03-01 23:16:17 +00:00
|
|
|
embed = (struct ng_source_embed_info *)msg->data;
|
|
|
|
bcopy(embed, &sc->embed_timestamp, sizeof(*embed));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_SOURCE_GET_TIMESTAMP:
|
|
|
|
{
|
|
|
|
struct ng_source_embed_info *embed;
|
|
|
|
|
2012-12-05 08:04:20 +00:00
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
|
2007-03-01 23:16:17 +00:00
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
embed = (struct ng_source_embed_info *)resp->data;
|
|
|
|
bcopy(&sc->embed_timestamp, embed, sizeof(*embed));
|
|
|
|
|
2007-03-02 01:44:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_SOURCE_SET_COUNTER:
|
|
|
|
{
|
|
|
|
struct ng_source_embed_cnt_info *embed;
|
|
|
|
|
2007-03-02 14:36:19 +00:00
|
|
|
if (msg->header.arglen != sizeof(*embed)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-03-02 01:44:04 +00:00
|
|
|
embed = (struct ng_source_embed_cnt_info *)msg->data;
|
|
|
|
if (embed->index >= NG_SOURCE_COUNTERS ||
|
|
|
|
!(embed->width == 1 || embed->width == 2 ||
|
|
|
|
embed->width == 4)) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
bcopy(embed, &sc->embed_counter[embed->index],
|
|
|
|
sizeof(*embed));
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NGM_SOURCE_GET_COUNTER:
|
|
|
|
{
|
|
|
|
uint8_t index = *(uint8_t *)msg->data;
|
|
|
|
struct ng_source_embed_cnt_info *embed;
|
|
|
|
|
|
|
|
if (index >= NG_SOURCE_COUNTERS) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-12-05 08:04:20 +00:00
|
|
|
NG_MKRESPONSE(resp, msg, sizeof(*embed), M_NOWAIT);
|
2007-03-02 01:44:04 +00:00
|
|
|
if (resp == NULL) {
|
|
|
|
error = ENOMEM;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
embed = (struct ng_source_embed_cnt_info *)resp->data;
|
|
|
|
bcopy(&sc->embed_counter[index], embed, sizeof(*embed));
|
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-31 23:03:09 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2002-11-05 01:08:11 +00:00
|
|
|
case NGM_ETHER_COOKIE:
|
|
|
|
if (!(msg->header.flags & NGF_RESP)) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (msg->header.cmd) {
|
2005-04-05 17:22:05 +00:00
|
|
|
case NGM_ETHER_GET_IFNAME:
|
|
|
|
{
|
|
|
|
char *ifname = (char *)msg->data;
|
|
|
|
|
|
|
|
if (msg->header.arglen < 2) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
2002-11-05 01:08:11 +00:00
|
|
|
}
|
2005-04-05 17:22:05 +00:00
|
|
|
|
|
|
|
if (ng_source_store_output_ifp(sc, ifname) == 0)
|
|
|
|
ng_source_set_autosrc(sc, 0);
|
2002-11-05 01:08:11 +00:00
|
|
|
break;
|
2005-04-05 17:22:05 +00:00
|
|
|
}
|
2002-11-05 01:08:11 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
}
|
|
|
|
break;
|
2002-10-31 23:03:09 +00:00
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2005-04-05 17:22:05 +00:00
|
|
|
/* Take care of synchronous response, if any. */
|
2002-11-02 02:29:43 +00:00
|
|
|
NG_RESPOND_MSG(error, node, item, resp);
|
2005-04-05 17:22:05 +00:00
|
|
|
/* Free the message and return. */
|
2002-11-02 02:29:43 +00:00
|
|
|
NG_FREE_MSG(msg);
|
2002-10-31 23:03:09 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive data on a hook
|
|
|
|
*
|
|
|
|
* If data comes in the input hook, enqueue it on the send queue.
|
|
|
|
* If data comes in the output hook, discard it.
|
|
|
|
*/
|
|
|
|
static int
|
2002-11-02 02:29:43 +00:00
|
|
|
ng_source_rcvdata(hook_p hook, item_p item)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
sc_p sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
2002-11-02 02:29:43 +00:00
|
|
|
struct mbuf *m;
|
2005-04-05 17:22:05 +00:00
|
|
|
int error = 0;
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2002-11-02 02:29:43 +00:00
|
|
|
NGI_GET_M(item, m);
|
|
|
|
NG_FREE_ITEM(item);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
|
|
|
/* Which hook? */
|
2005-04-05 17:22:05 +00:00
|
|
|
if (hook == sc->output) {
|
2002-10-31 23:03:09 +00:00
|
|
|
/* discard */
|
2002-11-02 02:29:43 +00:00
|
|
|
NG_FREE_M(m);
|
2002-10-31 23:03:09 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2005-04-05 17:22:05 +00:00
|
|
|
KASSERT(hook == sc->input, ("%s: no hook!", __func__));
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
/* Enqueue packet. */
|
2002-10-31 23:03:09 +00:00
|
|
|
/* XXX should we check IF_QFULL() ? */
|
2004-01-26 14:46:35 +00:00
|
|
|
_IF_ENQUEUE(&sc->snd_queue, m);
|
2002-10-31 23:03:09 +00:00
|
|
|
sc->queueOctets += m->m_pkthdr.len;
|
2007-03-02 01:44:04 +00:00
|
|
|
sc->last_packet = m;
|
2002-10-31 23:03:09 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shutdown processing
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_source_rmnode(node_p node)
|
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
sc_p sc = NG_NODE_PRIVATE(node);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
|
|
|
ng_source_stop(sc);
|
|
|
|
ng_source_clr_data(sc);
|
2002-11-02 02:29:43 +00:00
|
|
|
NG_NODE_SET_PRIVATE(node, NULL);
|
|
|
|
NG_NODE_UNREF(node);
|
2004-01-27 10:45:37 +00:00
|
|
|
free(sc, M_NETGRAPH);
|
2005-04-05 17:22:05 +00:00
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hook disconnection
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ng_source_disconnect(hook_p hook)
|
|
|
|
{
|
2002-11-02 01:26:28 +00:00
|
|
|
sc_p sc;
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2002-11-02 02:29:43 +00:00
|
|
|
sc = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
|
2004-01-27 10:45:37 +00:00
|
|
|
KASSERT(sc != NULL, ("%s: null node private", __func__));
|
2005-04-05 17:22:05 +00:00
|
|
|
if (NG_NODE_NUMHOOKS(NG_HOOK_NODE(hook)) == 0 || hook == sc->output)
|
2002-11-02 02:29:43 +00:00
|
|
|
ng_rmnode_self(NG_HOOK_NODE(hook));
|
2002-10-31 23:03:09 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2002-11-05 01:08:11 +00:00
|
|
|
/*
|
2011-02-21 09:01:34 +00:00
|
|
|
* Set sc->output_ifp to point to the struct ifnet of the interface
|
2002-11-05 01:08:11 +00:00
|
|
|
* reached via our output hook.
|
|
|
|
*/
|
|
|
|
static int
|
2005-04-05 17:22:05 +00:00
|
|
|
ng_source_store_output_ifp(sc_p sc, char *ifname)
|
2002-11-05 01:08:11 +00:00
|
|
|
{
|
|
|
|
struct ifnet *ifp;
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
ifp = ifunit(ifname);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
|
|
|
if (ifp == NULL) {
|
2008-09-21 07:33:33 +00:00
|
|
|
printf("%s: can't find interface %s\n", __func__, ifname);
|
2002-10-31 23:03:09 +00:00
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
sc->output_ifp = ifp;
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
/* XXX mucking with a drivers ifqueue size is ugly but we need it
|
|
|
|
* to queue a lot of packets to get close to line rate on a gigabit
|
|
|
|
* interface with small packets.
|
|
|
|
* XXX we should restore the original value at stop or disconnect
|
|
|
|
*/
|
2004-01-27 10:45:37 +00:00
|
|
|
if (ifp->if_snd.ifq_maxlen < NG_SOURCE_DRIVER_IFQ_MAXLEN) {
|
2002-10-31 23:03:09 +00:00
|
|
|
printf("ng_source: changing ifq_maxlen from %d to %d\n",
|
2002-11-02 01:26:28 +00:00
|
|
|
ifp->if_snd.ifq_maxlen, NG_SOURCE_DRIVER_IFQ_MAXLEN);
|
2002-10-31 23:03:09 +00:00
|
|
|
ifp->if_snd.ifq_maxlen = NG_SOURCE_DRIVER_IFQ_MAXLEN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the attached ethernet node's ethernet source address override flag.
|
|
|
|
*/
|
|
|
|
static int
|
2005-04-05 17:22:05 +00:00
|
|
|
ng_source_set_autosrc(sc_p sc, uint32_t flag)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
|
|
|
struct ng_mesg *msg;
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
NG_MKMESSAGE(msg, NGM_ETHER_COOKIE, NGM_ETHER_SET_AUTOSRC,
|
2005-04-05 17:22:05 +00:00
|
|
|
sizeof (uint32_t), M_NOWAIT);
|
2002-10-31 23:03:09 +00:00
|
|
|
if (msg == NULL)
|
|
|
|
return(ENOBUFS);
|
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
*(uint32_t *)msg->data = flag;
|
|
|
|
NG_SEND_MSG_HOOK(error, sc->node, msg, sc->output, 0);
|
2002-10-31 23:03:09 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear out the data we've queued
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_source_clr_data (sc_p sc)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
|
|
|
|
|
|
|
for (;;) {
|
2004-01-26 14:46:35 +00:00
|
|
|
_IF_DEQUEUE(&sc->snd_queue, m);
|
2002-10-31 23:03:09 +00:00
|
|
|
if (m == NULL)
|
|
|
|
break;
|
|
|
|
NG_FREE_M(m);
|
|
|
|
}
|
|
|
|
sc->queueOctets = 0;
|
2007-03-02 14:36:19 +00:00
|
|
|
sc->last_packet = NULL;
|
2002-10-31 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start sending queued data out the output hook
|
|
|
|
*/
|
2005-04-05 17:22:05 +00:00
|
|
|
static int
|
|
|
|
ng_source_start(sc_p sc, uint64_t packets)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
if (sc->output_ifp == NULL) {
|
|
|
|
printf("ng_source: start without iface configured\n");
|
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->node->nd_flags & NG_SOURCE_ACTIVE)
|
|
|
|
return (EBUSY);
|
|
|
|
|
|
|
|
sc->node->nd_flags |= NG_SOURCE_ACTIVE;
|
|
|
|
|
|
|
|
sc->packets = packets;
|
|
|
|
timevalclear(&sc->stats.elapsedTime);
|
|
|
|
timevalclear(&sc->stats.endTime);
|
|
|
|
getmicrotime(&sc->stats.startTime);
|
2005-12-23 19:14:38 +00:00
|
|
|
getmicrotime(&sc->stats.lastTime);
|
2005-04-05 17:22:05 +00:00
|
|
|
ng_callout(&sc->intr_ch, sc->node, NULL, 0,
|
|
|
|
ng_source_intr, sc, 0);
|
|
|
|
|
|
|
|
return (0);
|
2002-10-31 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stop sending queued data out the output hook
|
|
|
|
*/
|
|
|
|
static void
|
2005-04-05 17:22:05 +00:00
|
|
|
ng_source_stop(sc_p sc)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
2005-04-05 17:22:05 +00:00
|
|
|
ng_uncallout(&sc->intr_ch, sc->node);
|
|
|
|
sc->node->nd_flags &= ~NG_SOURCE_ACTIVE;
|
|
|
|
getmicrotime(&sc->stats.endTime);
|
|
|
|
sc->stats.elapsedTime = sc->stats.endTime;
|
|
|
|
timevalsub(&sc->stats.elapsedTime, &sc->stats.startTime);
|
2002-10-31 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* While active called every NG_SOURCE_INTR_TICKS ticks.
|
|
|
|
* Sends as many packets as the interface connected to our
|
|
|
|
* output hook is able to enqueue.
|
|
|
|
*/
|
|
|
|
static void
|
2004-01-30 15:34:57 +00:00
|
|
|
ng_source_intr(node_p node, hook_p hook, void *arg1, int arg2)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
2004-01-30 15:34:57 +00:00
|
|
|
sc_p sc = (sc_p)arg1;
|
2002-10-31 23:03:09 +00:00
|
|
|
struct ifqueue *ifq;
|
|
|
|
int packets;
|
|
|
|
|
2004-01-27 10:45:37 +00:00
|
|
|
KASSERT(sc != NULL, ("%s: null node private", __func__));
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
if (sc->packets == 0 || sc->output == NULL
|
2002-11-02 02:29:43 +00:00
|
|
|
|| (sc->node->nd_flags & NG_SOURCE_ACTIVE) == 0) {
|
2002-10-31 23:03:09 +00:00
|
|
|
ng_source_stop(sc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-01-26 14:53:16 +00:00
|
|
|
if (sc->output_ifp != NULL) {
|
2005-02-12 17:03:01 +00:00
|
|
|
ifq = (struct ifqueue *)&sc->output_ifp->if_snd;
|
2004-01-26 14:53:16 +00:00
|
|
|
packets = ifq->ifq_maxlen - ifq->ifq_len;
|
|
|
|
} else
|
|
|
|
packets = sc->snd_queue.ifq_len;
|
|
|
|
|
2005-12-23 19:14:38 +00:00
|
|
|
if (sc->stats.maxPps != 0) {
|
|
|
|
struct timeval now, elapsed;
|
|
|
|
uint64_t usec;
|
|
|
|
int maxpkt;
|
|
|
|
|
|
|
|
getmicrotime(&now);
|
|
|
|
elapsed = now;
|
|
|
|
timevalsub(&elapsed, &sc->stats.lastTime);
|
|
|
|
usec = elapsed.tv_sec * 1000000 + elapsed.tv_usec;
|
|
|
|
maxpkt = (uint64_t)sc->stats.maxPps * usec / 1000000;
|
|
|
|
sc->stats.lastTime = now;
|
|
|
|
if (packets > maxpkt)
|
|
|
|
packets = maxpkt;
|
|
|
|
}
|
|
|
|
|
2002-10-31 23:03:09 +00:00
|
|
|
ng_source_send(sc, packets, NULL);
|
2004-01-30 15:34:57 +00:00
|
|
|
if (sc->packets == 0)
|
2002-10-31 23:03:09 +00:00
|
|
|
ng_source_stop(sc);
|
2004-01-30 15:34:57 +00:00
|
|
|
else
|
2004-12-01 11:56:32 +00:00
|
|
|
ng_callout(&sc->intr_ch, node, NULL, NG_SOURCE_INTR_TICKS,
|
2004-11-02 20:26:41 +00:00
|
|
|
ng_source_intr, sc, 0);
|
2002-10-31 23:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-01-23 10:28:04 +00:00
|
|
|
* Send packets out our output hook.
|
2002-10-31 23:03:09 +00:00
|
|
|
*/
|
|
|
|
static int
|
2006-01-23 10:28:04 +00:00
|
|
|
ng_source_send(sc_p sc, int tosend, int *sent_p)
|
2002-10-31 23:03:09 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m, *m2;
|
2006-01-23 10:28:04 +00:00
|
|
|
int sent;
|
2002-10-31 23:03:09 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2004-01-27 10:45:37 +00:00
|
|
|
KASSERT(tosend >= 0, ("%s: negative tosend param", __func__));
|
2002-11-02 02:29:43 +00:00
|
|
|
KASSERT(sc->node->nd_flags & NG_SOURCE_ACTIVE,
|
2004-01-27 10:45:37 +00:00
|
|
|
("%s: inactive node", __func__));
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
if ((uint64_t)tosend > sc->packets)
|
2002-10-31 23:03:09 +00:00
|
|
|
tosend = sc->packets;
|
|
|
|
|
2006-01-23 10:28:04 +00:00
|
|
|
/* Go through the queue sending packets one by one. */
|
2002-10-31 23:03:09 +00:00
|
|
|
for (sent = 0; error == 0 && sent < tosend; ++sent) {
|
2004-01-26 14:46:35 +00:00
|
|
|
_IF_DEQUEUE(&sc->snd_queue, m);
|
2002-10-31 23:03:09 +00:00
|
|
|
if (m == NULL)
|
|
|
|
break;
|
|
|
|
|
2007-03-01 23:16:17 +00:00
|
|
|
/* Duplicate and modify the packet. */
|
|
|
|
error = ng_source_dup_mod(sc, m, &m2);
|
|
|
|
if (error) {
|
|
|
|
if (error == ENOBUFS)
|
|
|
|
_IF_PREPEND(&sc->snd_queue, m);
|
|
|
|
else
|
|
|
|
_IF_ENQUEUE(&sc->snd_queue, m);
|
2002-10-31 23:03:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-04-05 17:22:05 +00:00
|
|
|
/* Re-enqueue the original packet for us. */
|
2004-01-26 14:46:35 +00:00
|
|
|
_IF_ENQUEUE(&sc->snd_queue, m);
|
2002-10-31 23:03:09 +00:00
|
|
|
|
2006-01-23 10:28:04 +00:00
|
|
|
sc->stats.outFrames++;
|
|
|
|
sc->stats.outOctets += m2->m_pkthdr.len;
|
|
|
|
NG_SEND_DATA_ONLY(error, sc->output, m2);
|
|
|
|
if (error)
|
2002-10-31 23:03:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->packets -= sent;
|
|
|
|
if (sent_p != NULL)
|
|
|
|
*sent_p = sent;
|
|
|
|
return (error);
|
|
|
|
}
|
2007-03-01 23:16:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Modify packet in 'm' by changing 'len' bytes starting at 'offset'
|
|
|
|
* to data in 'cp'.
|
|
|
|
*
|
|
|
|
* The packet data in 'm' must be in a contiguous buffer in a single mbuf.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ng_source_packet_mod(sc_p sc, struct mbuf *m, int offset, int len, caddr_t cp,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Can't modify beyond end of packet. */
|
|
|
|
/* TODO: Pad packet for this case. */
|
|
|
|
if (offset + len > m->m_len)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bcopy(cp, mtod_off(m, offset, caddr_t), len);
|
|
|
|
}
|
|
|
|
|
2007-03-02 01:44:04 +00:00
|
|
|
static void
|
|
|
|
ng_source_mod_counter(sc_p sc, struct ng_source_embed_cnt_info *cnt,
|
|
|
|
struct mbuf *m, int increment)
|
|
|
|
{
|
|
|
|
caddr_t cp;
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
val = htonl(cnt->next_val);
|
|
|
|
cp = (caddr_t)&val + sizeof(val) - cnt->width;
|
|
|
|
ng_source_packet_mod(sc, m, cnt->offset, cnt->width, cp, cnt->flags);
|
|
|
|
|
|
|
|
if (increment) {
|
|
|
|
cnt->next_val += increment;
|
|
|
|
|
|
|
|
if (increment > 0 && cnt->next_val > cnt->max_val) {
|
|
|
|
cnt->next_val = cnt->min_val - 1 +
|
|
|
|
(cnt->next_val - cnt->max_val);
|
|
|
|
if (cnt->next_val > cnt->max_val)
|
|
|
|
cnt->next_val = cnt->max_val;
|
|
|
|
} else if (increment < 0 && cnt->next_val < cnt->min_val) {
|
|
|
|
cnt->next_val = cnt->max_val + 1 +
|
|
|
|
(cnt->next_val - cnt->min_val);
|
|
|
|
if (cnt->next_val < cnt->min_val)
|
|
|
|
cnt->next_val = cnt->max_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-01 23:16:17 +00:00
|
|
|
static int
|
|
|
|
ng_source_dup_mod(sc_p sc, struct mbuf *m0, struct mbuf **m_ptr)
|
|
|
|
{
|
|
|
|
struct mbuf *m;
|
2007-03-02 01:44:04 +00:00
|
|
|
struct ng_source_embed_cnt_info *cnt;
|
2007-03-01 23:16:17 +00:00
|
|
|
struct ng_source_embed_info *ts;
|
|
|
|
int modify;
|
|
|
|
int error = 0;
|
2007-03-02 01:44:04 +00:00
|
|
|
int i, increment;
|
2007-03-01 23:16:17 +00:00
|
|
|
|
|
|
|
/* Are we going to modify packets? */
|
|
|
|
modify = sc->embed_timestamp.flags & NGM_SOURCE_EMBED_ENABLE;
|
2007-03-02 01:44:04 +00:00
|
|
|
for (i = 0; !modify && i < NG_SOURCE_COUNTERS; ++i)
|
|
|
|
modify = sc->embed_counter[i].flags & NGM_SOURCE_EMBED_ENABLE;
|
2007-03-01 23:16:17 +00:00
|
|
|
|
|
|
|
/* Duplicate the packet. */
|
|
|
|
if (modify)
|
2012-12-05 08:04:20 +00:00
|
|
|
m = m_dup(m0, M_NOWAIT);
|
2007-03-01 23:16:17 +00:00
|
|
|
else
|
2012-12-05 08:04:20 +00:00
|
|
|
m = m_copypacket(m0, M_NOWAIT);
|
2007-03-01 23:16:17 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
error = ENOBUFS;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
*m_ptr = m;
|
|
|
|
|
|
|
|
if (!modify)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* Modify the copied packet for sending. */
|
|
|
|
KASSERT(M_WRITABLE(m), ("%s: packet not writable", __func__));
|
|
|
|
|
2007-03-02 01:44:04 +00:00
|
|
|
for (i = 0; i < NG_SOURCE_COUNTERS; ++i) {
|
|
|
|
cnt = &sc->embed_counter[i];
|
|
|
|
if (cnt->flags & NGM_SOURCE_EMBED_ENABLE) {
|
|
|
|
if ((cnt->flags & NGM_SOURCE_INC_CNT_PER_LIST) == 0 ||
|
|
|
|
sc->last_packet == m0)
|
|
|
|
increment = cnt->increment;
|
|
|
|
else
|
|
|
|
increment = 0;
|
|
|
|
ng_source_mod_counter(sc, cnt, m, increment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-01 23:16:17 +00:00
|
|
|
ts = &sc->embed_timestamp;
|
|
|
|
if (ts->flags & NGM_SOURCE_EMBED_ENABLE) {
|
|
|
|
struct timeval now;
|
|
|
|
getmicrotime(&now);
|
|
|
|
now.tv_sec = htonl(now.tv_sec);
|
|
|
|
now.tv_usec = htonl(now.tv_usec);
|
|
|
|
ng_source_packet_mod(sc, m, ts->offset, sizeof (now),
|
|
|
|
(caddr_t)&now, ts->flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
return(error);
|
|
|
|
}
|