aboutsummaryrefslogtreecommitdiff
path: root/example
diff options
context:
space:
mode:
authorPetri Savolainen <petri.savolainen@nokia.com>2016-02-09 14:25:10 +0200
committerMaxim Uvarov <maxim.uvarov@linaro.org>2016-03-03 22:32:04 +0300
commit6e1f610966ca1330c82d80129d3429b375e744bc (patch)
tree17195c804201ed4a9b3a86776d457b1cd0856629 /example
parentb6c21ca841467d0266ee974752b41d4492bcb802 (diff)
tests: use parse mac and ip address helpers
Convert some of the examples and tests to use MAC and IPv4 address parse functions instead of implementing those multiple times. There are still some examples to convert, but those would require a bit more effort. Signed-off-by: Petri Savolainen <petri.savolainen@nokia.com> Reviewed-by: Juha-Matti Tilli <juha-matti.tilli@nokia.com> Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
Diffstat (limited to 'example')
-rw-r--r--example/classifier/odp_classifier.c30
-rw-r--r--example/generator/odp_generator.c29
2 files changed, 11 insertions, 48 deletions
diff --git a/example/classifier/odp_classifier.c b/example/classifier/odp_classifier.c
index 57a58d979..6b81bd8f6 100644
--- a/example/classifier/odp_classifier.c
+++ b/example/classifier/odp_classifier.c
@@ -162,26 +162,6 @@ void print_cls_statistics(appl_args_t *args)
}
static inline
-int parse_ipv4_addr(const char *ipaddress, uint64_t *addr)
-{
- uint32_t b[4];
- int converted;
-
- converted = sscanf(ipaddress,
- "%" SCNu32 ".%" SCNu32 ".%" SCNu32 ".%" SCNu32 "",
- &b[3], &b[2], &b[1], &b[0]);
- if (4 != converted)
- return -1;
-
- if ((b[0] > 255) || (b[1] > 255) || (b[2] > 255) || (b[3] > 255))
- return -1;
-
- *addr = b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24;
-
- return 0;
-}
-
-static inline
int parse_mask(const char *str, uint64_t *mask)
{
uint64_t b;
@@ -722,6 +702,7 @@ static int parse_pmr_policy(appl_args_t *appl_args, char *argv[], char *optarg)
global_statistics *stats;
char *pmr_str;
uint32_t offset;
+ uint32_t ip_addr;
policy_count = appl_args->policy_count;
stats = appl_args->stats;
@@ -751,7 +732,14 @@ static int parse_pmr_policy(appl_args_t *appl_args, char *argv[], char *optarg)
token = strtok(NULL, ":");
strncpy(stats[policy_count].value, token,
DISPLAY_STRING_LEN - 1);
- parse_ipv4_addr(token, &stats[policy_count].rule.val);
+
+ if (odph_ipv4_addr_parse(&ip_addr, token)) {
+ EXAMPLE_ERR("Bad IP address\n");
+ exit(EXIT_FAILURE);
+ }
+
+ stats[policy_count].rule.val = ip_addr;
+
token = strtok(NULL, ":");
strncpy(stats[policy_count].mask, token,
DISPLAY_STRING_LEN - 1);
diff --git a/example/generator/odp_generator.c b/example/generator/odp_generator.c
index 3e5efc120..650007e22 100644
--- a/example/generator/odp_generator.c
+++ b/example/generator/odp_generator.c
@@ -103,7 +103,6 @@ static void parse_args(int argc, char *argv[], appl_args_t *appl_args);
static void print_info(char *progname, appl_args_t *appl_args);
static void usage(char *progname);
static int scan_ip(char *buf, unsigned int *paddr);
-static int scan_mac(char *in, odph_ethaddr_t *des);
static void tv_sub(struct timeval *recvtime, struct timeval *sendtime);
static void print_global_stats(int num_workers);
@@ -170,30 +169,6 @@ static int scan_ip(char *buf, unsigned int *paddr)
}
/**
- * Scan mac addr form string
- *
- * @param in mac string
- * @param des mac for odp_packet
- * @return 1 success, 0 failed
- */
-static int scan_mac(char *in, odph_ethaddr_t *des)
-{
- int field;
- int i;
- unsigned int mac[7];
-
- field = sscanf(in, "%2x:%2x:%2x:%2x:%2x:%2x",
- &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]);
-
- for (i = 0; i < 6; i++)
- des->addr[i] = mac[i];
-
- if (field != 6)
- return 0;
- return 1;
-}
-
-/**
* set up an udp packet
*
* @param pool Buffer pool to create packet in
@@ -987,14 +962,14 @@ static void parse_args(int argc, char *argv[], appl_args_t *appl_args)
break;
case 'a':
- if (scan_mac(optarg, &appl_args->srcmac) != 1) {
+ if (odph_eth_addr_parse(&appl_args->srcmac, optarg)) {
EXAMPLE_ERR("wrong src mac:%s\n", optarg);
exit(EXIT_FAILURE);
}
break;
case 'b':
- if (scan_mac(optarg, &appl_args->dstmac) != 1) {
+ if (odph_eth_addr_parse(&appl_args->dstmac, optarg)) {
EXAMPLE_ERR("wrong dst mac:%s\n", optarg);
exit(EXIT_FAILURE);
}