Initialize UserMetricsRecorder on Windows Ash and Ozone
[chromium-blink-merge.git] / net / tools / quic / quic_server.cc
blobfd24059d148f7940a31c2ba0c6a362ebc495d842
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/tools/quic/quic_server.h"
7 #include <errno.h>
8 #include <features.h>
9 #include <netinet/in.h>
10 #include <string.h>
11 #include <sys/epoll.h>
12 #include <sys/socket.h>
14 #include "net/base/ip_endpoint.h"
15 #include "net/quic/congestion_control/tcp_receiver.h"
16 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/quic_random.h"
18 #include "net/quic/quic_clock.h"
19 #include "net/quic/quic_crypto_stream.h"
20 #include "net/quic/quic_data_reader.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/tools/quic/quic_dispatcher.h"
23 #include "net/tools/quic/quic_in_memory_cache.h"
24 #include "net/tools/quic/quic_socket_utils.h"
26 #define MMSG_MORE 0
28 #ifndef SO_RXQ_OVFL
29 #define SO_RXQ_OVFL 40
30 #endif
32 namespace net {
33 namespace tools {
35 namespace {
37 const int kEpollFlags = EPOLLIN | EPOLLOUT | EPOLLET;
38 const char kSourceAddressTokenSecret[] = "secret";
40 } // namespace
42 QuicServer::QuicServer()
43 : port_(0),
44 fd_(-1),
45 packets_dropped_(0),
46 overflow_supported_(false),
47 use_recvmmsg_(false),
48 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()),
49 supported_versions_(QuicSupportedVersions()) {
50 Initialize();
53 QuicServer::QuicServer(const QuicConfig& config,
54 const QuicVersionVector& supported_versions)
55 : port_(0),
56 fd_(-1),
57 packets_dropped_(0),
58 overflow_supported_(false),
59 use_recvmmsg_(false),
60 config_(config),
61 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()),
62 supported_versions_(supported_versions) {
63 Initialize();
66 void QuicServer::Initialize() {
67 #if MMSG_MORE
68 use_recvmmsg_ = true;
69 #endif
70 epoll_server_.set_timeout_in_us(50 * 1000);
71 // Initialize the in memory cache now.
72 QuicInMemoryCache::GetInstance();
74 QuicEpollClock clock(&epoll_server_);
76 scoped_ptr<CryptoHandshakeMessage> scfg(
77 crypto_config_.AddDefaultConfig(
78 QuicRandom::GetInstance(), &clock,
79 QuicCryptoServerConfig::ConfigOptions()));
82 QuicServer::~QuicServer() {
85 bool QuicServer::Listen(const IPEndPoint& address) {
86 port_ = address.port();
87 int address_family = address.GetSockAddrFamily();
88 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
89 if (fd_ < 0) {
90 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno);
91 return false;
94 // Enable the socket option that allows the local address to be
95 // returned if the socket is bound to more than one address.
96 int rc = QuicSocketUtils::SetGetAddressInfo(fd_, address_family);
98 if (rc < 0) {
99 LOG(ERROR) << "IP detection not supported" << strerror(errno);
100 return false;
103 int get_overflow = 1;
104 rc = setsockopt(
105 fd_, SOL_SOCKET, SO_RXQ_OVFL, &get_overflow, sizeof(get_overflow));
107 if (rc < 0) {
108 DLOG(WARNING) << "Socket overflow detection not supported";
109 } else {
110 overflow_supported_ = true;
113 // These send and receive buffer sizes are sized for a single connection,
114 // because the default usage of QuicServer is as a test server with one or
115 // two clients. Adjust higher for use with many clients.
116 if (!QuicSocketUtils::SetReceiveBufferSize(fd_,
117 TcpReceiver::kReceiveWindowTCP)) {
118 return false;
121 if (!QuicSocketUtils::SetSendBufferSize(fd_,
122 TcpReceiver::kReceiveWindowTCP)) {
123 return false;
126 sockaddr_storage raw_addr;
127 socklen_t raw_addr_len = sizeof(raw_addr);
128 CHECK(address.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr),
129 &raw_addr_len));
130 rc = bind(fd_,
131 reinterpret_cast<const sockaddr*>(&raw_addr),
132 sizeof(raw_addr));
133 if (rc < 0) {
134 LOG(ERROR) << "Bind failed: " << strerror(errno);
135 return false;
138 DVLOG(1) << "Listening on " << address.ToString();
139 if (port_ == 0) {
140 SockaddrStorage storage;
141 IPEndPoint server_address;
142 if (getsockname(fd_, storage.addr, &storage.addr_len) != 0 ||
143 !server_address.FromSockAddr(storage.addr, storage.addr_len)) {
144 LOG(ERROR) << "Unable to get self address. Error: " << strerror(errno);
145 return false;
147 port_ = server_address.port();
148 DVLOG(1) << "Kernel assigned port is " << port_;
151 epoll_server_.RegisterFD(fd_, this, kEpollFlags);
152 dispatcher_.reset(CreateQuicDispatcher());
153 dispatcher_->Initialize(fd_);
155 return true;
158 QuicDispatcher* QuicServer::CreateQuicDispatcher() {
159 return new QuicDispatcher(
160 config_,
161 crypto_config_,
162 supported_versions_,
163 new QuicDispatcher::DefaultPacketWriterFactory(),
164 &epoll_server_);
167 void QuicServer::WaitForEvents() {
168 epoll_server_.WaitForEventsAndExecuteCallbacks();
171 void QuicServer::Shutdown() {
172 // Before we shut down the epoll server, give all active sessions a chance to
173 // notify clients that they're closing.
174 dispatcher_->Shutdown();
176 close(fd_);
177 fd_ = -1;
180 void QuicServer::OnEvent(int fd, EpollEvent* event) {
181 DCHECK_EQ(fd, fd_);
182 event->out_ready_mask = 0;
184 if (event->in_events & EPOLLIN) {
185 DVLOG(1) << "EPOLLIN";
186 bool read = true;
187 while (read) {
188 read = ReadAndDispatchSinglePacket(
189 fd_, port_, dispatcher_.get(),
190 overflow_supported_ ? &packets_dropped_ : nullptr);
193 if (event->in_events & EPOLLOUT) {
194 dispatcher_->OnCanWrite();
195 if (dispatcher_->HasPendingWrites()) {
196 event->out_ready_mask |= EPOLLOUT;
199 if (event->in_events & EPOLLERR) {
203 /* static */
204 bool QuicServer::ReadAndDispatchSinglePacket(int fd,
205 int port,
206 ProcessPacketInterface* processor,
207 QuicPacketCount* packets_dropped) {
208 // Allocate some extra space so we can send an error if the client goes over
209 // the limit.
210 char buf[2 * kMaxPacketSize];
212 IPEndPoint client_address;
213 IPAddressNumber server_ip;
214 int bytes_read =
215 QuicSocketUtils::ReadPacket(fd, buf, arraysize(buf),
216 packets_dropped,
217 &server_ip, &client_address);
219 if (bytes_read < 0) {
220 return false; // We failed to read.
223 QuicEncryptedPacket packet(buf, bytes_read, false);
225 IPEndPoint server_address(server_ip, port);
226 processor->ProcessPacket(server_address, client_address, packet);
228 return true;
231 } // namespace tools
232 } // namespace net