From 2ab562f1a46ffbc459146420caa86e400fa1b9e2 Mon Sep 17 00:00:00 2001 From: Vadim Kochan Date: Tue, 26 Jan 2016 22:25:01 +0200 Subject: trafgen: proto: Add function to set field from device MAC Add helper function to set device's MAC address to protocol field which may be used by Ethernet & ARP protocol header generation functions. Signed-off-by: Vadim Kochan Signed-off-by: Tobias Klauser --- trafgen_proto.c | 31 +++++++++++++++++++++++++++++++ trafgen_proto.h | 3 +++ 2 files changed, 34 insertions(+) diff --git a/trafgen_proto.c b/trafgen_proto.c index 15c48d4..cf2541f 100644 --- a/trafgen_proto.c +++ b/trafgen_proto.c @@ -5,7 +5,9 @@ #include #include +#include +#include "dev.h" #include "xmalloc.h" #include "trafgen_conf.h" #include "trafgen_proto.h" @@ -289,6 +291,35 @@ void proto_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t __proto_field_set_bytes(hdr, fid, (uint8_t *)&val, true, true); } +static void __proto_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid, + bool is_default) +{ + uint8_t mac[ETH_ALEN]; + int ret; + + if (proto_field_is_set(hdr, fid)) + return; + + if (!hdr->ctx->dev) + panic("Device is not specified\n"); + + ret = device_hw_address(hdr->ctx->dev, mac, sizeof(mac)); + if (ret < 0) + panic("Could not get device hw adress\n"); + + __proto_field_set_bytes(hdr, fid, mac, is_default, false); +} + +void proto_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid) +{ + __proto_field_set_dev_mac(hdr, fid, false); +} + +void proto_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid) +{ + __proto_field_set_dev_mac(hdr, fid, true); +} + void protos_init(const char *dev) { struct proto_hdr *p; diff --git a/trafgen_proto.h b/trafgen_proto.h index 8f1f201..517b278 100644 --- a/trafgen_proto.h +++ b/trafgen_proto.h @@ -94,4 +94,7 @@ extern void proto_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid, extern void proto_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val); +extern void proto_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid); +extern void proto_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid); + #endif /* TRAFGEN_PROTO_I_H */ -- cgit v1.2.3-54-g00ecf