]> git.proxmox.com Git - ceph.git/blame - ceph/src/msg/Messenger.cc
import quincy beta 17.1.0
[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 12
f67539c2 13Messenger *Messenger::create_client_messenger(CephContext *cct, std::string lname)
7c673cae 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 17 return Messenger::create(cct, public_msgr_type, entity_name_t::CLIENT(),
f67539c2 18 std::move(lname), nonce);
7c673cae
FG
19}
20
9f95a23c
TL
21uint64_t Messenger::get_pid_nonce()
22{
23 uint64_t nonce = getpid();
f67539c2 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
f67539c2
TL
36Messenger *Messenger::create(CephContext *cct, const std::string &type,
37 entity_name_t name, std::string lname,
38 uint64_t nonce)
7c673cae 39{
20effc67 40 if (type == "random" || type.find("async") != std::string::npos)
7c673cae 41 return new AsyncMessenger(cct, name, type, std::move(lname), nonce);
7c673cae
FG
42 lderr(cct) << "unrecognized ms_type '" << type << "'" << dendl;
43 return nullptr;
44}
45
11fdf7f2
TL
46/**
47 * Get the default crc flags for this messenger.
48 * but not yet dispatched.
49 */
50static int get_default_crc_flags(const ConfigProxy&);
51
52Messenger::Messenger(CephContext *cct_, entity_name_t w)
53 : trace_endpoint("0.0.0.0", 0, "Messenger"),
54 my_name(w),
55 default_send_priority(CEPH_MSG_PRIO_DEFAULT),
56 started(false),
57 magic(0),
58 socket_priority(-1),
59 cct(cct_),
60 crcflags(get_default_crc_flags(cct->_conf)),
20effc67
TL
61 auth_registry(cct),
62 comp_registry(cct)
11fdf7f2
TL
63{
64 auth_registry.refresh_config();
20effc67 65 comp_registry.refresh_config();
11fdf7f2
TL
66}
67
7c673cae
FG
68void Messenger::set_endpoint_addr(const entity_addr_t& a,
69 const entity_name_t &name)
70{
71 size_t hostlen;
72 if (a.get_family() == AF_INET)
73 hostlen = sizeof(struct sockaddr_in);
74 else if (a.get_family() == AF_INET6)
75 hostlen = sizeof(struct sockaddr_in6);
76 else
77 hostlen = 0;
78
79 if (hostlen) {
80 char buf[NI_MAXHOST] = { 0 };
81 getnameinfo(a.get_sockaddr(), hostlen, buf, sizeof(buf),
82 NULL, 0, NI_NUMERICHOST);
83
84 trace_endpoint.copy_ip(buf);
85 }
86 trace_endpoint.set_port(a.get_port());
87}
88
11fdf7f2
TL
89/**
90 * Get the default crc flags for this messenger.
91 * but not yet dispatched.
92 *
7c673cae
FG
93 * Pre-calculate desired software CRC settings. CRC computation may
94 * be disabled by default for some transports (e.g., those with strong
95 * hardware checksum support).
96 */
11fdf7f2 97int get_default_crc_flags(const ConfigProxy& conf)
7c673cae
FG
98{
99 int r = 0;
100 if (conf->ms_crc_data)
101 r |= MSG_CRC_DATA;
102 if (conf->ms_crc_header)
103 r |= MSG_CRC_HEADER;
104 return r;
105}
11fdf7f2
TL
106
107int Messenger::bindv(const entity_addrvec_t& addrs)
108{
109 return bind(addrs.legacy_addr());
110}
111