ovn-controller: Handle physical changes correctly
[cascardo/ovs.git] / ovn / lib / ovn-dhcp.h
1 /*
2  * Copyright (c) 2016 Red Hat, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef OVN_DHCP_H
18 #define OVN_DHCP_H 1
19
20 #include "hmap.h"
21 #include "hash.h"
22
23 struct dhcp_opts_map {
24     struct hmap_node hmap_node;
25     char *name;
26     char *type;
27     size_t code;
28 };
29
30 #define DHCP_OPTION(NAME, CODE, TYPE) \
31     {.name = NAME, .code = CODE, .type = TYPE}
32
33 #define OFFERIP              DHCP_OPTION("offerip", 0, "ipv4")
34 #define DHCP_OPT_NETMASK     DHCP_OPTION("netmask", 1, "ipv4")
35 #define DHCP_OPT_ROUTER      DHCP_OPTION("router", 3, "ipv4")
36 #define DHCP_OPT_DNS_SERVER  DHCP_OPTION("dns_server", 6, "ipv4")
37 #define DHCP_OPT_LOG_SERVER  DHCP_OPTION("log_server", 7, "ipv4")
38 #define DHCP_OPT_LPR_SERVER  DHCP_OPTION("lpr_server", 9, "ipv4")
39 #define DHCP_OPT_SWAP_SERVER DHCP_OPTION("swap_server", 16, "ipv4")
40
41 #define DHCP_OPT_POLICY_FILTER \
42     DHCP_OPTION("policy_filter", 21, "ipv4")
43
44 #define DHCP_OPT_ROUTER_SOLICITATION \
45     DHCP_OPTION("router_solicitation", 32, "ipv4")
46
47 #define DHCP_OPT_NIS_SERVER  DHCP_OPTION("nis_server", 41, "ipv4")
48 #define DHCP_OPT_NTP_SERVER  DHCP_OPTION("ntp_server", 42, "ipv4")
49 #define DHCP_OPT_SERVER_ID   DHCP_OPTION("server_id", 54, "ipv4")
50 #define DHCP_OPT_TFTP_SERVER DHCP_OPTION("tftp_server", 66, "ipv4")
51
52 #define DHCP_OPT_CLASSLESS_STATIC_ROUTE \
53     DHCP_OPTION("classless_static_route", 121, "static_routes")
54 #define DHCP_OPT_MS_CLASSLESS_STATIC_ROUTE \
55     DHCP_OPTION("ms_classless_static_route", 249, "static_routes")
56
57 #define DHCP_OPT_IP_FORWARD_ENABLE DHCP_OPTION("ip_forward_enable", 19, "bool")
58 #define DHCP_OPT_ROUTER_DISCOVERY DHCP_OPTION("router_discovery", 31, "bool")
59 #define DHCP_OPT_ETHERNET_ENCAP DHCP_OPTION("ethernet_encap", 36, "bool")
60
61 #define DHCP_OPT_DEFAULT_TTL DHCP_OPTION("default_ttl", 23, "uint8")
62
63 #define DHCP_OPT_TCP_TTL  DHCP_OPTION("tcp_ttl", 37, "uint8")
64 #define DHCP_OPT_MTU      DHCP_OPTION("mtu", 26, "uint16")
65 #define DHCP_OPT_LEASE_TIME DHCP_OPTION("lease_time", 51, "uint32")
66 #define DHCP_OPT_T1 DHCP_OPTION("T1", 58, "uint32")
67 #define DHCP_OPT_T2 DHCP_OPTION("T2", 59, "uint32")
68
69 static inline uint32_t
70 dhcp_opt_hash(char *opt_name)
71 {
72     return hash_string(opt_name, 0);
73 }
74
75 static inline struct dhcp_opts_map *
76 dhcp_opts_find(const struct hmap *dhcp_opts, char *opt_name)
77 {
78     struct dhcp_opts_map *dhcp_opt;
79     HMAP_FOR_EACH_WITH_HASH (dhcp_opt, hmap_node, dhcp_opt_hash(opt_name),
80                              dhcp_opts) {
81         if (!strcmp(dhcp_opt->name, opt_name)) {
82             return dhcp_opt;
83         }
84     }
85
86     return NULL;
87 }
88
89 static inline void
90 dhcp_opt_add(struct hmap *dhcp_opts, char *opt_name, size_t code, char *type)
91 {
92     struct dhcp_opts_map *dhcp_opt = xzalloc(sizeof *dhcp_opt);
93     dhcp_opt->name = xstrdup(opt_name);
94     dhcp_opt->code = code;
95     dhcp_opt->type = xstrdup(type);
96     hmap_insert(dhcp_opts, &dhcp_opt->hmap_node, dhcp_opt_hash(opt_name));
97 }
98
99 static inline void
100 dhcp_opts_destroy(struct hmap *dhcp_opts)
101 {
102     struct dhcp_opts_map *dhcp_opt;
103     HMAP_FOR_EACH_POP(dhcp_opt, hmap_node, dhcp_opts) {
104         free(dhcp_opt->name);
105         free(dhcp_opt->type);
106         free(dhcp_opt);
107     }
108     hmap_destroy(dhcp_opts);
109 }
110
111 #endif /* OVN_DHCP_H */