Skip to content

Commit

Permalink
Merge pull request #326 from s41m0n/dynmon/fix_example
Browse files Browse the repository at this point in the history
Fixed Dynmon example and logging strings
  • Loading branch information
frisso committed Sep 4, 2020
2 parents 486e96b + 99c8505 commit b775876
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/services/pcn-dynmon/examples/ntp_packets_counter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"ingress-path": {
"name": "NTP Amplification BUA probe",
"code": "\r\n#include <uapi/linux/ip.h>\r\n#include <uapi/linux/udp.h>\r\n\r\n#define IP_PROTO_UDP 17\r\n#define NTP_PORT 123\r\n\r\nstruct eth_hdr {\r\n __be64 dst : 48;\r\n __be64 src : 48;\r\n __be16 proto;\r\n} __attribute__((packed));\r\n\r\nBPF_ARRAY(NTP_PACKETS_COUNTER, uint64_t,1);\r\n\r\nstatic __always_inline\r\nint handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {\r\n /*Parsing L2*/\r\n void *data = (void *) (long) ctx->data;\r\n void *data_end = (void *) (long) ctx->data_end;\r\n struct eth_hdr *ethernet = data;\r\n if (data + sizeof(*ethernet) > data_end)\r\n return RX_OK;\r\n\r\n if (ethernet->proto != bpf_htons(ETH_P_IP))\r\n return RX_OK;\r\n\r\n /*Parsing L3*/\r\n struct iphdr *ip = data + sizeof(struct eth_hdr);\r\n if (data + sizeof(struct eth_hdr) + sizeof(*ip) > data_end)\r\n return RX_OK;\r\n if ((int) ip->version != 4)\r\n return RX_OK;\r\n\r\n if (ip->protocol != IP_PROTO_UDP)\r\n return RX_OK;\r\n\r\n /*Parsing L4*/\r\n uint8_t ip_header_len = 4 * ip->ihl;\r\n struct udphdr *udp = data + sizeof(*ethernet) + ip_header_len;\r\n if (data + sizeof(*ethernet) + ip_header_len + sizeof(*udp) > data_end)\r\n return RX_OK;\r\n\r\n if (udp->source == bpf_htons(NTP_PORT) || udp->dest == bpf_htons(NTP_PORT)) {\r\n pcn_log(ctx, LOG_TRACE, \"%I:%P\\t-> %I:%P\", ip->saddr,udp->source,ip->daddr,udp->dest);\r\n unsigned int key = 0;\r\n uint64_t * ntp_pckts_counter = NTP_PACKETS_COUNTER.lookup(&key);\r\n if (!ntp_pckts_counter)\r\n pcn_log(ctx, LOG_ERR, \"[NTP_AMP_BUA] Unable to find NTP_PACKETS_COUNTER map\");\r\n else\r\n *ntp_pckts_counter+=1;\r\n }\r\n\r\n return RX_OK;\r\n}",
"metrics": [
"metric-configs": [
{
"name": "ntp_packets_total",
"map-name": "NTP_PACKETS_COUNTER",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"ingress-path": {
"name": "NTP Amplification WARNING probe",
"code": "\r\n#include <uapi/linux/ip.h>\r\n#include <uapi/linux/udp.h>\r\n\r\n#define IP_PROTO_UDP 17\r\n#define NTP_PORT 123\r\n#define NTP_MODE_PRIVATE 7\r\n\r\n#define MODE(li_vn_mode) (uint8_t) ((li_vn_mode & 0x07) >> 0)\r\n\r\nstruct eth_hdr {\r\n __be64 dst : 48;\r\n __be64 src : 48;\r\n __be16 proto;\r\n} __attribute__((packed));\r\n\r\nstruct ntp_packet {\r\n uint8_t li_vn_mode;\r\n uint8_t stratum;\r\n uint8_t poll;\r\n uint8_t precision;\r\n uint32_t rootDelay;\r\n uint32_t rootDispersion;\r\n uint32_t refId;\r\n uint32_t refTm_s;\r\n uint32_t refTm_f;\r\n uint32_t origTm_s;\r\n uint32_t origTm_f;\r\n uint32_t rxTm_s;\r\n uint32_t rxTm_f;\r\n uint32_t txTm_s;\r\n uint32_t txTm_f;\r\n} __attribute__((packed));\r\n\r\nBPF_ARRAY(NTP_PACKETS_COUNTER, uint64_t, 1);\r\nBPF_ARRAY(NTP_MODE_PRIVATE_PACKETS_COUNTER, uint64_t, 1);\r\n\r\nstatic __always_inline\r\nint handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {\r\n /*Parsing L2*/\r\n void *data = (void *) (long) ctx->data;\r\n void *data_end = (void *) (long) ctx->data_end;\r\n struct eth_hdr *ethernet = data;\r\n if (data + sizeof(*ethernet) > data_end)\r\n return RX_OK;\r\n\r\n if (ethernet->proto != bpf_htons(ETH_P_IP))\r\n return RX_OK;\r\n\r\n /*Parsing L3*/\r\n struct iphdr *ip = data + sizeof(struct eth_hdr);\r\n if (data + sizeof(struct eth_hdr) + sizeof(*ip) > data_end)\r\n return RX_OK;\r\n if ((int) ip->version != 4)\r\n return RX_OK;\r\n\r\n if (ip->protocol != IP_PROTO_UDP)\r\n return RX_OK;\r\n\r\n /*Parsing L4*/\r\n uint8_t ip_header_len = 4 * ip->ihl;\r\n struct udphdr *udp = data + sizeof(*ethernet) + ip_header_len;\r\n if (data + sizeof(*ethernet) + ip_header_len + sizeof(*udp) > data_end)\r\n return RX_OK;\r\n\r\n if (udp->source == bpf_htons(NTP_PORT) || udp->dest == bpf_htons(NTP_PORT)) {\r\n pcn_log(ctx, LOG_TRACE, \"%I:%P\\t-> %I:%P\", ip->saddr,udp->source,ip->daddr,udp->dest);\r\n unsigned int key = 0;\r\n uint64_t * ntp_pckts_counter = NTP_PACKETS_COUNTER.lookup(&key);\r\n if (!ntp_pckts_counter)\r\n pcn_log(ctx, LOG_ERR, \"[NTP_AMP_WARNING] Unable to find NTP_PACKETS_COUNTER map\");\r\n else\r\n *ntp_pckts_counter+=1;\r\n\r\n /*Parsing NTP*/\r\n struct ntp_packet *ntp = data + sizeof(*ethernet) + ip_header_len + sizeof(struct udphdr);\r\n if (data + sizeof(*ethernet) + ip_header_len + sizeof(struct udphdr) + sizeof(*ntp) > data_end)\r\n return RX_OK;\r\n\r\n uint8_t mode = MODE(ntp->li_vn_mode);\r\n pcn_log(ctx, LOG_TRACE, \"NTP mode: %hhu\", mode);\r\n if (mode == NTP_MODE_PRIVATE) {\r\n pcn_log(ctx, LOG_TRACE, \"RECEIVED NTP MODE 7\");\r\n key = 0;\r\n uint64_t * ntp_mode_private_counter = NTP_MODE_PRIVATE_PACKETS_COUNTER.lookup(&key);\r\n if (!ntp_mode_private_counter)\r\n pcn_log(ctx, LOG_ERR, \"[NTP_AMP_WARNING] Unable to find NTP_MODE_PRIVATE_PACKETS_COUNTER map\");\r\n else\r\n *ntp_mode_private_counter+=1;\r\n }\r\n }\r\n\r\n return RX_OK;\r\n}",
"metrics": [
"metric-configs": [
{
"name": "ntp_packets_total",
"map-name": "NTP_PACKETS_COUNTER",
Expand Down
2 changes: 1 addition & 1 deletion src/services/pcn-dynmon/examples/packet_counter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"ingress-path": {
"name": "Packets counter probe",
"code": "\r\n BPF_ARRAY(PKT_COUNTER, uint64_t, 1);\r\n static __always_inline int handle_rx(struct CTXTYPE *ctx, struct pkt_metadata *md) {\r\n unsigned int key = 0;\r\n uint64_t *pkt_counter = PKT_COUNTER.lookup(&key);\r\n if (!pkt_counter){\r\n /*counter map not found !? */\r\n return RX_OK;\r\n }\r\n *pkt_counter+=1;\r\n pcn_log(ctx, LOG_TRACE, \"counter: %d\", *pkt_counter);\r\n return RX_OK;\r\n }",
"metrics": [
"metric-configs": [
{
"name": "packets_total",
"map-name": "PKT_COUNTER",
Expand Down
7 changes: 4 additions & 3 deletions src/services/pcn-dynmon/src/Dynmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ std::shared_ptr<Metrics> Dynmon::getMetrics() {
}

std::shared_ptr<Metric> Dynmon::getEgressMetric(const std::string &name) {
logger()->debug("[Dynmon] getEgressMetrics()");
logger()->debug("[Dynmon] getEgressMetric()");
auto egressPathConfig = m_dpConfig->getEgressPathConfig();
auto metricConfig = egressPathConfig->getMetricConfig(name);

Expand Down Expand Up @@ -307,7 +307,7 @@ std::shared_ptr<Metric> Dynmon::getIngressMetric(const std::string &name) {
}

std::vector<std::shared_ptr<Metric>> Dynmon::getIngressMetrics() {
logger()->debug("[Dynmon] getEgressMetrics()");
logger()->debug("[Dynmon] getIngressMetrics()");
std::vector<std::shared_ptr<Metric>> metrics;
auto ingressMetricConfigs =
m_dpConfig->getIngressPathConfig()->getMetricConfigsList();
Expand Down Expand Up @@ -420,7 +420,7 @@ std::string Dynmon::getEgressOpenMetrics() {

/*UNUSED: left here for future endpoints development*/
std::string Dynmon::getIngressOpenMetrics() {
logger()->debug("[Dynmon] getEgressMetrics()");
logger()->debug("[Dynmon] getIngressOpenMetrics()");
std::vector<std::string> metrics;
auto ingressMetricConfigs =
m_dpConfig->getIngressPathConfig()->getMetricConfigsList();
Expand Down Expand Up @@ -565,6 +565,7 @@ std::string Dynmon::do_get_open_metric(const shared_ptr<MetricConfig>& config, P

auto extractionOptions = config->getExtractionOptions();
auto mapName = config->getMapName();

int index = type == ProgramType::INGRESS ? ingressSwapState.getProgramIndexAndMapNameToRead(mapName) :
egressSwapState.getProgramIndexAndMapNameToRead(mapName);

Expand Down
8 changes: 4 additions & 4 deletions src/services/pcn-dynmon/src/swap/CodeRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,13 @@ void CodeRewriter::compile(std::string &original_code, ProgramType type,
/*If is enabled, try enhanced compilation or basic compilation if it has failed.*/
if(!is_enabled) {
config = {};
logger->info("[Dynmon_CodeRewriter] No map marked as swappable, no compilation performed");
logger->info("[Dynmon_CodeRewriter] No map marked as swappable, no rewrites performed");
} else if(try_enhance_compilation(original_code, type, maps_to_swap, config)) {
logger->info("[Dynmon_CodeRewriter] Successfully compiled with ENHANCED tuning");
logger->info("[Dynmon_CodeRewriter] Successfully rewritten with ENHANCED tuning");
} else if(do_base_compile(original_code, maps_to_swap, config)) {
logger->info("[Dynmon_CodeRewriter] Successfully compiled with BASE tuning");
logger->info("[Dynmon_CodeRewriter] Successfully rewritten with BASE tuning");
} else {
config = {};
logger->info("[Dynmon_CodeRewriter] Unable to perform compilation, no optimization performed");
logger->info("[Dynmon_CodeRewriter] Error while trying to rewrite the code, no rewrites performed");
}
}

0 comments on commit b775876

Please sign in to comment.