udpfw: dynamic allocation of netbe_* structures
[tldk.git] / examples / udpfwd / parse.h
1 /*
2  * Copyright (c) 2016  Intel Corporation.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __PARSE_H__
17 #define __PARSE_H__
18
19 #define PARSE_LIST_DELIM "-"
20
21 union parse_val {
22         uint64_t u64;
23         struct {
24                 uint16_t family;
25                 union {
26                         struct in_addr addr4;
27                         struct in6_addr addr6;
28                 };
29         } in;
30         struct ether_addr mac;
31         rte_cpuset_t cpuset;
32 };
33
34 static int
35 parse_uint_val(__rte_unused const char *key, const char *val, void *prm)
36 {
37         union parse_val *rv;
38         unsigned long v;
39         char *end;
40
41         rv = prm;
42         errno = 0;
43         v = strtoul(val, &end, 0);
44         if (errno != 0 || end[0] != 0 || v > UINT32_MAX)
45                 return -EINVAL;
46
47         rv->u64 = v;
48         return 0;
49 }
50
51 static const char *
52 format_addr(const struct sockaddr_storage *sp, char buf[], size_t len)
53 {
54         const struct sockaddr_in *i4;
55         const struct sockaddr_in6 *i6;
56         const void *addr;
57
58         if (sp->ss_family == AF_INET) {
59                 i4 = (const struct sockaddr_in *)sp;
60                 addr = &i4->sin_addr;
61         } else if (sp->ss_family == AF_INET6) {
62                 i6 = (const struct sockaddr_in6 *)sp;
63                 addr = &i6->sin6_addr;
64         } else
65                 return NULL;
66
67
68         return inet_ntop(sp->ss_family, addr, buf, len);
69 }
70
71 int parse_netbe_arg(struct netbe_port *prt, const char *arg);
72
73 int netbe_parse_dest(const char *fname, struct netbe_dest_prm *prm);
74
75 int netfe_parse_cfg(const char *fname, struct netfe_lcore_prm *lp);
76
77 #endif /* __PARSE_H__ */
78