]> git.proxmox.com Git - ceph.git/blame - ceph/src/msg/Messenger.cc
import ceph 15.2.13
[ceph.git] / ceph / src / msg / Messenger.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
7c673cae 4#include <netdb.h>
7c673cae
FG
5
6#include "include/types.h"
11fdf7f2
TL
7#include "include/random.h"
8
7c673cae
FG
9#include "Messenger.h"
10
7c673cae 11#include "msg/async/AsyncMessenger.h"
7c673cae
FG
12
13Messenger *Messenger::create_client_messenger(CephContext *cct, string lname)
14{
11fdf7f2 15 std::string public_msgr_type = cct->_conf->ms_public_type.empty() ? cct->_conf.get_val<std::string>("ms_type") : cct->_conf->ms_public_type;
9f95a23c 16 auto nonce = get_random_nonce();
7c673cae
FG
17 return Messenger::create(cct, public_msgr_type, entity_name_t::CLIENT(),
18 std::move(lname), nonce, 0);
19}
20
9f95a23c
TL
21uint64_t Messenger::get_pid_nonce()
22{
23 uint64_t nonce = getpid();
7f7e6c64 24 if (nonce == 1 || getenv("CEPH_USE_RANDOM_NONCE")) {
9f95a23c
TL
25 // we're running in a container; use a random number instead!
26 nonce = ceph::util::generate_random_number<uint64_t>();
27 }
28 return nonce;
29}
30
31uint64_t Messenger::get_random_nonce()
32{
33 return ceph::util::generate_random_number<uint64_t>();
34}
35
7c673cae
FG
36Messenger *Messenger::create(CephContext *cct, const string &type,
37 entity_name_t name, string lname,
38 uint64_t nonce, uint64_t cflags)
39{
40 int r = -1;
41 if (type == "random") {
9f95a23c
TL
42 r = 0;
43 //r = ceph::util::generate_random_number(0, 1);
7c673cae 44 }
9f95a23c 45 if (r == 0 || type.find("async") != std::string::npos)
7c673cae 46 return new AsyncMessenger(cct, name, type, std::move(lname), nonce);
7c673cae
FG
47 lderr(cct) << "unrecognized ms_type '" << type << "'" << dendl;
48 return nullptr;
49}
50
11fdf7f2
TL
51/**
52 * Get the default crc flags for this messenger.
53 * but not yet dispatched.
54 */
55static int get_default_crc_flags(const ConfigProxy&);
56
57Messenger::Messenger(CephContext *cct_, entity_name_t w)
58 : trace_endpoint("0.0.0.0", 0, "Messenger"),
59 my_name(w),
60 default_send_priority(CEPH_MSG_PRIO_DEFAULT),
61 started(false),
62 magic(0),
63 socket_priority(-1),
64 cct(cct_),
65 crcflags(get_default_crc_flags(cct->_conf)),
66 auth_registry(cct)
67{
68 auth_registry.refresh_config();
69}
70
7c673cae
FG
71void Messenger::set_endpoint_addr(const entity_addr_t& a,
72 const entity_name_t &name)
73{
74 size_t hostlen;
75 if (a.get_family() == AF_INET)
76 hostlen = sizeof(struct sockaddr_in);
77 else if (a.get_family() == AF_INET6)
78 hostlen = sizeof(struct sockaddr_in6);
79 else
80 hostlen = 0;
81
82 if (hostlen) {
83 char buf[NI_MAXHOST] = { 0 };
84 getnameinfo(a.get_sockaddr(), hostlen, buf, sizeof(buf),
85 NULL, 0, NI_NUMERICHOST);
86
87 trace_endpoint.copy_ip(buf);
88 }
89 trace_endpoint.set_port(a.get_port());
90}
91
11fdf7f2
TL
92/**
93 * Get the default crc flags for this messenger.
94 * but not yet dispatched.
95 *
7c673cae
FG
96 * Pre-calculate desired software CRC settings. CRC computation may
97 * be disabled by default for some transports (e.g., those with strong
98 * hardware checksum support).
99 */
11fdf7f2 100int get_default_crc_flags(const ConfigProxy& conf)
7c673cae
FG
101{
102 int r = 0;
103 if (conf->ms_crc_data)
104 r |= MSG_CRC_DATA;
105 if (conf->ms_crc_header)
106 r |= MSG_CRC_HEADER;
107 return r;
108}
11fdf7f2
TL
109
110int Messenger::bindv(const entity_addrvec_t& addrs)
111{
112 return bind(addrs.legacy_addr());
113}
114