Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: net/quic/quic_server.cc

Issue 340433002: Port QuicServer to Chrome network stack (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Additional comments Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/tools/quic/quic_server.h" 5 #include "net/quic/quic_server.h"
6 6
7 #include <errno.h>
8 #include <features.h>
9 #include <netinet/in.h>
10 #include <string.h> 7 #include <string.h>
11 #include <sys/epoll.h>
12 #include <sys/socket.h>
13 8
14 #include "net/base/ip_endpoint.h" 9 #include "net/base/ip_endpoint.h"
10 #include "net/base/net_errors.h"
15 #include "net/quic/congestion_control/tcp_receiver.h" 11 #include "net/quic/congestion_control/tcp_receiver.h"
16 #include "net/quic/crypto/crypto_handshake.h" 12 #include "net/quic/crypto/crypto_handshake.h"
17 #include "net/quic/crypto/quic_random.h" 13 #include "net/quic/crypto/quic_random.h"
18 #include "net/quic/quic_clock.h"
19 #include "net/quic/quic_crypto_stream.h" 14 #include "net/quic/quic_crypto_stream.h"
20 #include "net/quic/quic_data_reader.h" 15 #include "net/quic/quic_data_reader.h"
16 #include "net/quic/quic_dispatcher.h"
17 #include "net/quic/quic_in_memory_cache.h"
21 #include "net/quic/quic_protocol.h" 18 #include "net/quic/quic_protocol.h"
22 #include "net/tools/quic/quic_in_memory_cache.h" 19 #include "net/quic/quic_server_packet_writer.h"
23 #include "net/tools/quic/quic_socket_utils.h" 20 #include "net/tools/quic/quic_socket_utils.h"
24 21
25 #define MMSG_MORE 0
26
27 #ifndef SO_RXQ_OVFL
28 #define SO_RXQ_OVFL 40
29 #endif
30
31 const int kEpollFlags = EPOLLIN | EPOLLOUT | EPOLLET;
32 static const char kSourceAddressTokenSecret[] = "secret";
33 const uint32 kServerInitialFlowControlWindow = 100 * net::kMaxPacketSize;
34 22
35 namespace net { 23 namespace net {
36 namespace tools {
37 24
38 QuicServer::QuicServer() 25 static const char kSourceAddressTokenSecret[] = "secret";
39 : port_(0), 26
40 fd_(-1), 27 // Allocate some extra space so we can send an error if the client goes over
41 packets_dropped_(0), 28 // the limit.
42 overflow_supported_(false), 29 static const int kReadBufferSize = 2 * net::kMaxPacketSize;
43 use_recvmmsg_(false),
44 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()),
45 supported_versions_(QuicSupportedVersions()) {
46 // Use hardcoded crypto parameters for now.
47 config_.SetDefaults();
48 Initialize();
49 }
50 30
51 QuicServer::QuicServer(const QuicConfig& config, 31 QuicServer::QuicServer(const QuicConfig& config,
52 const QuicVersionVector& supported_versions) 32 const QuicVersionVector& supported_versions)
53 : port_(0), 33 : helper_(new QuicConnectionHelper(
54 fd_(-1), 34 base::MessageLoop::current()->message_loop_proxy().get(),
55 packets_dropped_(0), 35 &clock_,
56 overflow_supported_(false), 36 QuicRandom::GetInstance())),
57 use_recvmmsg_(false),
58 config_(config), 37 config_(config),
59 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()), 38 crypto_config_(kSourceAddressTokenSecret, QuicRandom::GetInstance()),
60 supported_versions_(supported_versions) { 39 supported_versions_(supported_versions),
40 read_pending_(false),
41 synchronous_read_count_(0),
42 read_buffer_(new IOBufferWithSize(kReadBufferSize)),
43 weak_factory_(this) {
61 Initialize(); 44 Initialize();
62 } 45 }
63 46
64 void QuicServer::Initialize() { 47 void QuicServer::Initialize() {
65 #if MMSG_MORE
66 use_recvmmsg_ = true;
67 #endif
68 epoll_server_.set_timeout_in_us(50 * 1000);
69 // Initialize the in memory cache now. 48 // Initialize the in memory cache now.
70 QuicInMemoryCache::GetInstance(); 49 QuicInMemoryCache::GetInstance();
71 50
72 QuicEpollClock clock(&epoll_server_);
73
74 scoped_ptr<CryptoHandshakeMessage> scfg( 51 scoped_ptr<CryptoHandshakeMessage> scfg(
75 crypto_config_.AddDefaultConfig( 52 crypto_config_.AddDefaultConfig(QuicRandom::GetInstance(),
76 QuicRandom::GetInstance(), &clock, 53 helper_->GetClock(),
77 QuicCryptoServerConfig::ConfigOptions())); 54 QuicCryptoServerConfig::ConfigOptions()));
78
79 // Set flow control options in the config.
80 config_.SetInitialCongestionWindowToSend(kServerInitialFlowControlWindow);
81 } 55 }
82 56
83 QuicServer::~QuicServer() { 57 QuicServer::~QuicServer() {
84 } 58 }
85 59
86 bool QuicServer::Listen(const IPEndPoint& address) { 60 int QuicServer::Listen(const IPEndPoint& address) {
87 port_ = address.port(); 61 scoped_ptr<UDPServerSocket> socket(
88 int address_family = address.GetSockAddrFamily(); 62 new UDPServerSocket(NULL, NetLog::Source()));
89 fd_ = socket(address_family, SOCK_DGRAM | SOCK_NONBLOCK, IPPROTO_UDP);
90 if (fd_ < 0) {
91 LOG(ERROR) << "CreateSocket() failed: " << strerror(errno);
92 return false;
93 }
94 63
95 int rc = QuicSocketUtils::SetGetAddressInfo(fd_, address_family); 64 // TODO(dmz): socket->AllowBroadcast()? socket->SetMulticastInterface?
65 socket->AllowAddressReuse();
96 66
67 int rc = socket->Listen(address);
97 if (rc < 0) { 68 if (rc < 0) {
98 LOG(ERROR) << "IP detection not supported" << strerror(errno); 69 LOG(ERROR) << "Listen() failed: " << strerror(errno);
99 return false; 70 return rc;
100 }
101
102 int get_overflow = 1;
103 rc = setsockopt(
104 fd_, SOL_SOCKET, SO_RXQ_OVFL, &get_overflow, sizeof(get_overflow));
105
106 if (rc < 0) {
107 DLOG(WARNING) << "Socket overflow detection not supported";
108 } else {
109 overflow_supported_ = true;
110 } 71 }
111 72
112 // These send and receive buffer sizes are sized for a single connection, 73 // These send and receive buffer sizes are sized for a single connection,
113 // because the default usage of QuicServer is as a test server with one or 74 // because the default usage of QuicServer is as a test server with one or
114 // two clients. Adjust higher for use with many clients. 75 // two clients. Adjust higher for use with many clients.
115 if (!QuicSocketUtils::SetReceiveBufferSize(fd_, 76 rc = socket->SetReceiveBufferSize(TcpReceiver::kReceiveWindowTCP);
116 TcpReceiver::kReceiveWindowTCP)) { 77 if (rc < 0) {
117 return false; 78 LOG(ERROR) << "SetReceiveBufferSize() failed: " << strerror(errno);
79 return rc;
118 } 80 }
119 81
120 if (!QuicSocketUtils::SetSendBufferSize(fd_, 82 rc = socket->SetSendBufferSize(TcpReceiver::kReceiveWindowTCP);
121 TcpReceiver::kReceiveWindowTCP)) { 83 if (rc < 0) {
122 return false; 84 LOG(ERROR) << "SetSendBufferSize() failed: " << strerror(errno);
85 return rc;
123 } 86 }
124 87
125 // Enable the socket option that allows the local address to be
126 // returned if the socket is bound to more than on address.
127 int get_local_ip = 1;
128 rc = setsockopt(fd_, IPPROTO_IP, IP_PKTINFO,
129 &get_local_ip, sizeof(get_local_ip));
130 if (rc == 0 && address_family == AF_INET6) {
131 rc = setsockopt(fd_, IPPROTO_IPV6, IPV6_RECVPKTINFO,
132 &get_local_ip, sizeof(get_local_ip));
133 }
134 if (rc != 0) {
135 LOG(ERROR) << "Failed to set required socket options";
136 return false;
137 }
138 88
139 sockaddr_storage raw_addr; 89 socket->GetLocalAddress(&server_address_);
140 socklen_t raw_addr_len = sizeof(raw_addr);
141 CHECK(address.ToSockAddr(reinterpret_cast<sockaddr*>(&raw_addr),
142 &raw_addr_len));
143 rc = bind(fd_,
144 reinterpret_cast<const sockaddr*>(&raw_addr),
145 sizeof(raw_addr));
146 if (rc < 0) {
147 LOG(ERROR) << "Bind failed: " << strerror(errno);
148 return false;
149 }
150 90
151 DVLOG(1) << "Listening on " << address.ToString(); 91 DVLOG(1) << "Listening on " << server_address_.ToString();
152 if (port_ == 0) {
153 SockaddrStorage storage;
154 IPEndPoint server_address;
155 if (getsockname(fd_, storage.addr, &storage.addr_len) != 0 ||
156 !server_address.FromSockAddr(storage.addr, storage.addr_len)) {
157 LOG(ERROR) << "Unable to get self address. Error: " << strerror(errno);
158 return false;
159 }
160 port_ = server_address.port();
161 DVLOG(1) << "Kernel assigned port is " << port_;
162 }
163 92
164 epoll_server_.RegisterFD(fd_, this, kEpollFlags); 93 socket_.swap(socket);
165 dispatcher_.reset(CreateQuicDispatcher());
166 dispatcher_->Initialize(fd_);
167 94
168 return true; 95 dispatcher_.reset(
169 } 96 new QuicDispatcher(config_,
97 crypto_config_,
98 supported_versions_,
99 helper_));
100 QuicServerPacketWriter* writer = new QuicServerPacketWriter(dispatcher_.get(),
101 socket_.get());
102 dispatcher_->Initialize(writer);
170 103
171 QuicDispatcher* QuicServer::CreateQuicDispatcher() { 104 StartReading();
172 return new QuicDispatcher(
173 config_,
174 crypto_config_,
175 supported_versions_,
176 &epoll_server_);
177 }
178 105
179 void QuicServer::WaitForEvents() { 106 return OK;
180 epoll_server_.WaitForEventsAndExecuteCallbacks();
181 } 107 }
182 108
183 void QuicServer::Shutdown() { 109 void QuicServer::Shutdown() {
184 // Before we shut down the epoll server, give all active sessions a chance to 110 // Before we shut down the epoll server, give all active sessions a chance to
185 // notify clients that they're closing. 111 // notify clients that they're closing.
186 dispatcher_->Shutdown(); 112 dispatcher_->Shutdown();
187 113
188 close(fd_); 114 socket_->Close();
189 fd_ = -1;
190 } 115 }
191 116
192 void QuicServer::OnEvent(int fd, EpollEvent* event) { 117 void QuicServer::StartReading() {
193 DCHECK_EQ(fd, fd_); 118 if (read_pending_) {
194 event->out_ready_mask = 0; 119 return;
120 }
195 121
196 if (event->in_events & EPOLLIN) { 122 int result = socket_->RecvFrom(
197 DVLOG(1) << "EPOLLIN"; 123 read_buffer_.get(),
198 bool read = true; 124 read_buffer_->size(),
199 while (read) { 125 &read_addr_,
200 read = ReadAndDispatchSinglePacket( 126 base::Bind(&QuicServer::OnReadComplete, base::Unretained(this)));
201 fd_, port_, dispatcher_.get(), 127
202 overflow_supported_ ? &packets_dropped_ : NULL); 128 if (result == ERR_IO_PENDING) {
203 } 129 read_pending_ = true;
130 return;
204 } 131 }
205 if (event->in_events & EPOLLOUT) { 132
206 dispatcher_->OnCanWrite(); 133 if (++synchronous_read_count_ > 32) {
207 if (dispatcher_->HasPendingWrites()) { 134 synchronous_read_count_ = 0;
208 event->out_ready_mask |= EPOLLOUT; 135 // Schedule the processing through the message loop to 1) prevent infinite
209 } 136 // recursion and 2) avoid blocking the thread for too long.
210 } 137 base::MessageLoop::current()->PostTask(
211 if (event->in_events & EPOLLERR) { 138 FROM_HERE,
139 base::Bind(&QuicServer::OnReadComplete,
140 weak_factory_.GetWeakPtr(),
141 result));
142 } else {
143 OnReadComplete(result);
212 } 144 }
213 } 145 }
214 146
215 /* static */ 147 void QuicServer::OnReadComplete(int result) {
216 bool QuicServer::ReadAndDispatchSinglePacket(int fd, 148 read_pending_ = false;
217 int port, 149 if (result == 0)
218 QuicDispatcher* dispatcher, 150 result = ERR_CONNECTION_CLOSED; // TODO(dmz): correct for server?
219 uint32* packets_dropped) {
220 // Allocate some extra space so we can send an error if the client goes over
221 // the limit.
222 char buf[2 * kMaxPacketSize];
223 151
224 IPEndPoint client_address; 152 if (result < 0) {
225 IPAddressNumber server_ip; 153 // TODO(dmz) how to handle error? probably just DVLOG, ignore, & continue?
226 int bytes_read = 154 return;
227 QuicSocketUtils::ReadPacket(fd, buf, arraysize(buf),
228 packets_dropped,
229 &server_ip, &client_address);
230
231 if (bytes_read < 0) {
232 return false; // We failed to read.
233 } 155 }
234 156
235 QuicEncryptedPacket packet(buf, bytes_read, false); 157 QuicEncryptedPacket packet(read_buffer_->data(), result, false);
158 dispatcher_->ProcessPacket(server_address_, read_addr_, packet);
236 159
237 IPEndPoint server_address(server_ip, port); 160 StartReading();
238 dispatcher->ProcessPacket(server_address, client_address, packet);
239
240 return true;
241 } 161 }
242 162
243 } // namespace tools
244 } // namespace net 163 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698