1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2012 Inktank
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.
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"
23 #define dout_subsys ceph_subsys_
25 const struct sockaddr *find_ip_in_subnet_list(
27 const struct ifaddrs *ifa,
28 const std::string &networks,
29 const std::string &interfaces)
31 std::list<string> nets;
32 get_str_list(networks, nets);
33 std::list<string> ifs;
34 get_str_list(interfaces, ifs);
36 // filter interfaces by name
37 const struct ifaddrs *filtered = 0;
42 lderr(cct) << "interface names specified but not network names" << dendl;
45 const struct ifaddrs *t = ifa;
46 struct ifaddrs *head = 0;
50 if (strcmp(i.c_str(), t->ifa_name) == 0) {
56 struct ifaddrs *n = new ifaddrs;
57 memcpy(n, t, sizeof(*t));
64 lderr(cct) << "no interfaces matching " << ifs << dendl;
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;
75 if (!parse_network(s->c_str(), &net, &prefix_len)) {
76 lderr(cct) << "unable to parse network: " << *s << dendl;
80 const struct ifaddrs *found = find_ip_in_subnet(
82 (struct sockaddr *) &net, prefix_len);
89 if (filtered != ifa) {
91 struct ifaddrs *t = filtered->ifa_next;
100 // observe this change
101 struct Observer : public md_config_obs_t {
103 explicit Observer(const char *c) {
108 const char** get_tracked_conf_keys() const override {
109 return (const char **)keys;
111 void handle_conf_change(const struct md_config_t *conf,
112 const std::set <std::string> &changed) override {
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)
123 const struct sockaddr *found = find_ip_in_subnet_list(cct, ifa, networks,
126 lderr(cct) << "unable to find any IP address in networks '" << networks
127 << "' interfaces '" << interfaces << "'" << dendl;
131 char buf[INET6_ADDRSTRLEN];
134 err = getnameinfo(found,
135 (found->sa_family == AF_INET)
136 ? sizeof(struct sockaddr_in)
137 : sizeof(struct sockaddr_in6),
143 lderr(cct) << "unable to convert chosen address to string: " << gai_strerror(err) << dendl;
147 Observer obs(conf_var);
149 cct->_conf->add_observer(&obs);
151 cct->_conf->set_val_or_die(conf_var, buf);
152 cct->_conf->apply_changes(NULL);
154 cct->_conf->remove_observer(&obs);
157 void pick_addresses(CephContext *cct, int needs)
160 int r = getifaddrs(&ifa);
162 string err = cpp_strerror(errno);
163 lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl;
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"),
175 if ((needs & CEPH_PICK_ADDRESS_CLUSTER)
176 && cct->_conf->cluster_addr.is_blank_ip()) {
177 if (!cct->_conf->cluster_network.empty()) {
179 cct, ifa, cct->_conf->cluster_network,
180 cct->_conf->get_val<string>("cluster_network_interface"),
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;
187 cct, ifa, cct->_conf->public_network,
188 cct->_conf->get_val<string>("public_network_interface"),
198 std::string pick_iface(CephContext *cct, const struct sockaddr_storage &network)
201 int r = getifaddrs(&ifa);
203 string err = cpp_strerror(errno);
204 lderr(cct) << "unable to fetch interfaces and addresses: " << err << dendl;
208 unsigned int prefix_len = 0;
209 const struct ifaddrs *found = find_ip_in_subnet(ifa,
210 (const struct sockaddr *) &network, prefix_len);
214 result = found->ifa_name;
223 bool have_local_addr(CephContext *cct, const list<entity_addr_t>& ls, entity_addr_t *match)
226 int r = getifaddrs(&ifa);
228 lderr(cct) << "unable to fetch interfaces and addresses: " << cpp_strerror(errno) << dendl;
233 for (struct ifaddrs *addrs = ifa; addrs != NULL; addrs = addrs->ifa_next) {
234 if (addrs->ifa_addr) {
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)) {