On Thu, Aug 11, 2016 at 06:16:27PM +0200, Tobias Klauser wrote:
> Currently struct proto_hdr is used twofold:
>
> 1) Statically define protocol behavior, i.e. all the *_hdr definitions in
> trafgen_l{2,3,4}.c which map a protocol id/layer to a set of callback
> functions.
>
> 2) For each packet created at parse time the struct is memcpy()'ed
> (including all the static information from 1) and then used to store
> dynamic information at parse/run time.
>
> Thus, struct proto_hdr members such as the proto id, layer and the
> pointers callback functions get copied for each created packet (in
> addition to the other fields which get changed during parsing). Also,
> static/dynamic information get mixed and we e.g. can't make the protocol
> definitions const to ensure they'll not get changed by mistake.
>
> Rather than copying the struct proto_hdr for every packet, clearly
> separate the two purposes defined above by splitting struct proto_hdr
> into two structs:
>
> 1) struct proto_ops for the static (const) protocol behavior definition
>
> 2) struct proto_hdr (reduced) for dynamic information
>
> struct proto_hdr keeps a pointer to the corresponding proto_ops instance
> and uses it to execute the corresponding callbacks.
You can add my "Acked-by" if it is needed.
>
> Reference:
> https://groups.google.com/forum/#!msg/netsniff-ng/20RvwJdh50Y/eMkbmKSaBgAJ
> Signed-off-by: Tobias Klauser <[email protected]>
> ---
> trafgen_l2.c | 24 ++++++++++-----------
> trafgen_l3.c | 8 +++----
> trafgen_l4.c | 22 +++++++++----------
> trafgen_parser.y | 2 +-
> trafgen_proto.c | 65
> +++++++++++++++++++++++++++++---------------------------
> trafgen_proto.h | 40 +++++++++++++++++-----------------
> 6 files changed, 83 insertions(+), 78 deletions(-)
>
> diff --git a/trafgen_l2.c b/trafgen_l2.c
> index 1863332b3543..f09b2a61cacc 100644
> --- a/trafgen_l2.c
> +++ b/trafgen_l2.c
> @@ -47,7 +47,7 @@ static void eth_header_init(struct proto_hdr *hdr)
> proto_field_set_default_dev_mac(hdr, ETH_SRC_ADDR);
> }
>
>
> -struct proto_hdr *proto_header_init(enum proto_id pid)
> +struct proto_hdr *proto_header_new(enum proto_id pid)
The old name was really bad by may be something like:
{proto,packet}_header_{add,push}
might be better ?
> {
> struct proto_hdr **headers = current_packet()->headers;
> - const struct proto_hdr *hdr = proto_header_by_id(pid);
> - struct proto_hdr *new_hdr;
> + const struct proto_ops *ops = proto_ops_by_id(pid);
> + struct proto_hdr *hdr;
>
> bug_on(current_packet()->headers_count >= PROTO_MAX_LAYERS);
>
> - new_hdr = xmalloc(sizeof(*new_hdr));
> - memcpy(new_hdr, hdr, sizeof(*new_hdr));
> + hdr = xzmalloc(sizeof(*hdr));
> + hdr->ops = ops;
> + hdr->pkt_id = current_packet_id();
>
> - new_hdr->pkt_id = current_packet_id();
> + if (ops && ops->header_init)
> + ops->header_init(hdr);
>
> - if (new_hdr->header_init)
> - new_hdr->header_init(new_hdr);
> + headers[current_packet()->headers_count++] = hdr;
>
> - headers[current_packet()->headers_count++] = new_hdr;
> - return new_hdr;
> + return hdr;
> }
>
Thanks!
Vadim Kochan,
--
You received this message because you are subscribed to the Google Groups
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.