]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/pick_address.cc
update sources to v12.2.3
[ceph.git] / ceph / src / common / pick_address.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2004-2012 Inktank
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #include "common/pick_address.h"
16 #include "include/ipaddr.h"
17 #include "include/str_list.h"
18 #include "common/debug.h"
19 #include "common/errno.h"
20
21 #include <netdb.h>
22
23 #define dout_subsys ceph_subsys_
24
25 const struct sockaddr *find_ip_in_subnet_list(
26 CephContext *cct,
27 const struct ifaddrs *ifa,
28 const std::string &networks,
29 const std::string &interfaces)
30 {
31 std::list<string> nets;
32 get_str_list(networks, nets);
33 std::list<string> ifs;
34 get_str_list(interfaces, ifs);
35
36 // filter interfaces by name
37 const struct ifaddrs *filtered = 0;
38 if (ifs.empty()) {
39 filtered = ifa;
40 } else {
41 if (nets.empty()) {
42 lderr(cct) << "interface names specified but not network names" << dendl;
43 exit(1);
44 }
45 const struct ifaddrs *t = ifa;
46 struct ifaddrs *head = 0;
47 while (t != NULL) {
48 bool match = false;
49 for (auto& i : ifs) {
50 if (strcmp(i.c_str(), t->ifa_name) == 0) {
51 match = true;
52 break;
53 }
54 }
55 if (match) {
56 struct ifaddrs *n = new ifaddrs;
57 memcpy(n, t, sizeof(*t));
58 n->ifa_next = head;
59 head = n;
60 }
61 t = t->ifa_next;
62 }
63 if (head == NULL) {
64 lderr(cct) << "no interfaces matching " << ifs << dendl;
65 exit(1);
66 }
67 filtered = head;
68 }
69
70 struct sockaddr *r = NULL;
71 for (std::list<string>::iterator s = nets.begin(); s != nets.end(); ++s) {
72 struct sockaddr_storage net;
73 unsigned int prefix_len;
74
75 if (!parse_network(s->c_str(), &net, &prefix_len)) {
76 lderr(cct) << "unable to parse network: " << *s << dendl;
77 exit(1);
78 }
79
80 const struct ifaddrs *found = find_ip_in_subnet(
81 filtered,
82 (struct sockaddr *) &net, prefix_len);
83 if (found) {
84 r = found->ifa_addr;
85 break;
86 }
87 }
88
89 if (filtered != ifa) {
90 while (filtered) {
91 struct ifaddrs *t = filtered->ifa_next;
92 delete filtered;
93 filtered = t;
94 }
95 }
96
97 return r;
98 }
99
100 // observe this change
101 struct Observer : public md_config_obs_t {
102 const char *keys[2];
103 explicit Observer(const char *c) {
104 keys[0] = c;
105 keys[1] = NULL;
106 }
107
108 const char** get_tracked_conf_keys() const override {
109 return (const char **)keys;
110 }
111 void handle_conf_change(const struct md_config_t *conf,
112 const std::set <std::string> &changed) override {
113 // do nothing.
114 }
115 };
116
117 static void fill_in_one_address(CephContext *cct,
118 const struct ifaddrs *ifa,
119 const string networks,
120 const string interfaces,
121 const char *conf_var)
122 {
123 const struct sockaddr *found = find_ip_in_subnet_list(cct, ifa, networks,
124 interfaces);
125 if (!found) {
126 lderr(cct) << "unable to find any IP address in networks '" << networks
127 << "' interfaces '" << interfaces << "'" << dendl;
128 exit(1);
129 }
130
131 char buf[INET6_ADDRSTRLEN];
132 int err;
133
134 err = getnameinfo(found,
135 (found->sa_family == AF_INET)
136 ? sizeof(struct sockaddr_in)
137 : sizeof(struct sockaddr_in6),
138
139 buf, sizeof(buf),
140 NULL, 0,
141 NI_NUMERICHOST);
142 if (err != 0) {
143 lderr(cct) << "unable to convert chosen address to string: " << gai_strerror(err) << dendl;
144 exit(1);
145 }
146
147 Observer obs(conf_var);
148
149 cct->_conf->add_observer(&obs);
150
151 cct->_conf->set_val_or_die(conf_var, buf);
152 cct->_conf->apply_changes(NULL);
153
154 cct->_conf->remove_observer(&obs);
155 }
156
157 void pick_addresses(CephContext *cct, int needs)
158 {
159 struct ifaddrs *ifa;
160 int r = getifaddrs(&ifa);
161 if (r<0) {
162 string err = cpp_strerror(errno);
163 lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl;
164 exit(1);
165 }
166
167 if ((needs & CEPH_PICK_ADDRESS_PUBLIC)
168 && cct->_conf->public_addr.is_blank_ip()
169 && !cct->_conf->public_network.empty()) {
170 fill_in_one_address(cct, ifa, cct->_conf->public_network,
171 cct->_conf->get_val<string>("public_network_interface"),
172 "public_addr");
173 }
174
175 if ((needs & CEPH_PICK_ADDRESS_CLUSTER)
176 && cct->_conf->cluster_addr.is_blank_ip()) {
177 if (!cct->_conf->cluster_network.empty()) {
178 fill_in_one_address(
179 cct, ifa, cct->_conf->cluster_network,
180 cct->_conf->get_val<string>("cluster_network_interface"),
181 "cluster_addr");
182 } else {
183 if (!cct->_conf->public_network.empty()) {
184 lderr(cct) << "Public network was set, but cluster network was not set " << dendl;
185 lderr(cct) << " Using public network also for cluster network" << dendl;
186 fill_in_one_address(
187 cct, ifa, cct->_conf->public_network,
188 cct->_conf->get_val<string>("public_network_interface"),
189 "cluster_addr");
190 }
191 }
192 }
193
194 freeifaddrs(ifa);
195 }
196
197
198 std::string pick_iface(CephContext *cct, const struct sockaddr_storage &network)
199 {
200 struct ifaddrs *ifa;
201 int r = getifaddrs(&ifa);
202 if (r < 0) {
203 string err = cpp_strerror(errno);
204 lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl;
205 return {};
206 }
207
208 const unsigned int prefix_len = max(sizeof(in_addr::s_addr), sizeof(in6_addr::s6_addr)) * CHAR_BIT;
209 const struct ifaddrs *found = find_ip_in_subnet(ifa,
210 (const struct sockaddr *) &network, prefix_len);
211
212 std::string result;
213 if (found) {
214 result = found->ifa_name;
215 }
216
217 freeifaddrs(ifa);
218
219 return result;
220 }
221
222
223 bool have_local_addr(CephContext *cct, const list<entity_addr_t>& ls, entity_addr_t *match)
224 {
225 struct ifaddrs *ifa;
226 int r = getifaddrs(&ifa);
227 if (r < 0) {
228 lderr(cct) << "unable to fetch interfaces and addresses: " << cpp_strerror(errno) << dendl;
229 exit(1);
230 }
231
232 bool found = false;
233 for (struct ifaddrs *addrs = ifa; addrs != NULL; addrs = addrs->ifa_next) {
234 if (addrs->ifa_addr) {
235 entity_addr_t a;
236 a.set_sockaddr(addrs->ifa_addr);
237 for (list<entity_addr_t>::const_iterator p = ls.begin(); p != ls.end(); ++p) {
238 if (a.is_same_host(*p)) {
239 *match = *p;
240 found = true;
241 goto out;
242 }
243 }
244 }
245 }
246
247 out:
248 freeifaddrs(ifa);
249 return found;
250 }