ipfs_impl.go 11.4 KB
Newer Older
1 2 3
package network

import (
4
	"context"
5
	"errors"
6
	"fmt"
Jeromy's avatar
Jeromy committed
7
	"io"
8
	"sync/atomic"
9
	"time"
Jeromy's avatar
Jeromy committed
10

Jeromy's avatar
Jeromy committed
11 12 13 14
	bsmsg "github.com/ipfs/go-bitswap/message"

	cid "github.com/ipfs/go-cid"
	logging "github.com/ipfs/go-log"
Raúl Kripalani's avatar
Raúl Kripalani committed
15 16 17 18 19
	"github.com/libp2p/go-libp2p-core/connmgr"
	"github.com/libp2p/go-libp2p-core/host"
	"github.com/libp2p/go-libp2p-core/network"
	"github.com/libp2p/go-libp2p-core/peer"
	peerstore "github.com/libp2p/go-libp2p-core/peerstore"
20
	"github.com/libp2p/go-libp2p-core/protocol"
Raúl Kripalani's avatar
Raúl Kripalani committed
21
	"github.com/libp2p/go-libp2p-core/routing"
22
	"github.com/libp2p/go-libp2p/p2p/protocol/ping"
Steven Allen's avatar
Steven Allen committed
23
	msgio "github.com/libp2p/go-msgio"
Jeromy's avatar
Jeromy committed
24
	ma "github.com/multiformats/go-multiaddr"
25
	"github.com/multiformats/go-multistream"
26 27
)

Jeromy's avatar
Jeromy committed
28
var log = logging.Logger("bitswap_network")
Jeromy's avatar
Jeromy committed
29

30
var connectTimeout = time.Second * 5
31 32
var sendMessageTimeout = time.Minute * 10

33
// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host.
34
func NewFromIpfsHost(host host.Host, r routing.ContentRouting, opts ...NetOpt) BitSwapNetwork {
dirkmc's avatar
dirkmc committed
35
	s := processSettings(opts...)
36

37
	bitswapNetwork := impl{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38
		host:    host,
39
		routing: r,
40

dirkmc's avatar
dirkmc committed
41 42 43 44 45 46
		protocolBitswapNoVers:  s.ProtocolPrefix + ProtocolBitswapNoVers,
		protocolBitswapOneZero: s.ProtocolPrefix + ProtocolBitswapOneZero,
		protocolBitswapOneOne:  s.ProtocolPrefix + ProtocolBitswapOneOne,
		protocolBitswap:        s.ProtocolPrefix + ProtocolBitswap,

		supportedProtocols: s.SupportedProtocols,
47
	}
48

49
	return &bitswapNetwork
50 51
}

dirkmc's avatar
dirkmc committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
func processSettings(opts ...NetOpt) Settings {
	s := Settings{
		SupportedProtocols: []protocol.ID{
			ProtocolBitswap,
			ProtocolBitswapOneOne,
			ProtocolBitswapOneZero,
			ProtocolBitswapNoVers,
		},
	}
	for _, opt := range opts {
		opt(&s)
	}
	for i, proto := range s.SupportedProtocols {
		s.SupportedProtocols[i] = s.ProtocolPrefix + proto
	}
	return s
}

70 71
// impl transforms the ipfs network interface, which sends and receives
// NetMessage objects, into the bitswap network interface.
72
type impl struct {
Steven Allen's avatar
Steven Allen committed
73 74 75 76
	// NOTE: Stats must be at the top of the heap allocation to ensure 64bit
	// alignment.
	stats Stats

77 78 79
	host          host.Host
	routing       routing.ContentRouting
	connectEvtMgr *connectEventManager
80

dirkmc's avatar
dirkmc committed
81 82 83 84 85 86
	protocolBitswapNoVers  protocol.ID
	protocolBitswapOneZero protocol.ID
	protocolBitswapOneOne  protocol.ID
	protocolBitswap        protocol.ID

	supportedProtocols []protocol.ID
87

88 89
	// inbound messages from the network are forwarded to the receiver
	receiver Receiver
90 91
}

Jeromy's avatar
Jeromy committed
92
type streamMessageSender struct {
93 94 95 96 97
	to        peer.ID
	stream    network.Stream
	connected bool
	bsnet     *impl
	opts      *MessageSenderOpts
Jeromy's avatar
Jeromy committed
98 99
}

100 101
// Open a stream to the remote peer
func (s *streamMessageSender) Connect(ctx context.Context) (network.Stream, error) {
102
	if s.connected {
103 104 105
		return s.stream, nil
	}

106 107 108 109
	tctx, cancel := context.WithTimeout(ctx, s.opts.SendTimeout)
	defer cancel()

	if err := s.bsnet.ConnectTo(tctx, s.to); err != nil {
110 111 112
		return nil, err
	}

113
	stream, err := s.bsnet.newStreamToPeer(tctx, s.to)
114 115
	if err != nil {
		return nil, err
116
	}
117 118

	s.stream = stream
119
	s.connected = true
120
	return s.stream, nil
Jeromy's avatar
Jeromy committed
121 122
}

123
// Reset the stream
124
func (s *streamMessageSender) Reset() error {
125 126
	if s.stream != nil {
		err := s.stream.Reset()
127
		s.connected = false
128 129 130
		return err
	}
	return nil
131 132
}

133
// Close the stream
134
func (s *streamMessageSender) Close() error {
135
	return s.stream.Close()
136 137
}

138
// Indicates whether the peer supports HAVE / DONT_HAVE messages
dirkmc's avatar
dirkmc committed
139
func (s *streamMessageSender) SupportsHave() bool {
140 141 142
	return s.bsnet.SupportsHave(s.stream.Protocol())
}

143
// Send a message to the peer, attempting multiple times
144
func (s *streamMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMessage) error {
145 146
	return s.multiAttempt(ctx, func() error {
		return s.send(ctx, msg)
147 148 149 150
	})
}

// Perform a function with multiple attempts, and a timeout
151
func (s *streamMessageSender) multiAttempt(ctx context.Context, fn func() error) error {
152
	// Try to call the function repeatedly
153 154
	var err error
	for i := 0; i < s.opts.MaxRetries; i++ {
155
		if err = fn(); err == nil {
156
			// Attempt was successful
157 158 159
			return nil
		}

160 161
		// Attempt failed

162 163 164
		// If the sender has been closed or the context cancelled, just bail out
		select {
		case <-ctx.Done():
Dirk McCormick's avatar
Dirk McCormick committed
165
			return ctx.Err()
166 167 168
		default:
		}

169 170 171 172 173 174
		// Protocol is not supported, so no need to try multiple times
		if errors.Is(err, multistream.ErrNotSupported) {
			s.bsnet.connectEvtMgr.MarkUnresponsive(s.to)
			return err
		}

175 176 177
		// Failed to send so reset stream and try again
		_ = s.Reset()

178
		// Failed too many times so mark the peer as unresponsive and return an error
179
		if i == s.opts.MaxRetries-1 {
180 181 182 183 184 185
			s.bsnet.connectEvtMgr.MarkUnresponsive(s.to)
			return err
		}

		select {
		case <-ctx.Done():
Dirk McCormick's avatar
Dirk McCormick committed
186
			return ctx.Err()
187 188 189 190 191 192 193 194
		case <-time.After(s.opts.SendErrorBackoff):
			// wait a short time in case disconnect notifications are still propagating
			log.Infof("send message to %s failed but context was not Done: %s", s.to, err)
		}
	}
	return err
}

195 196
// Send a message to the peer
func (s *streamMessageSender) send(ctx context.Context, msg bsmsg.BitSwapMessage) error {
197
	start := time.Now()
198
	stream, err := s.Connect(ctx)
199 200 201 202 203
	if err != nil {
		log.Infof("failed to open stream to %s: %s", s.to, err)
		return err
	}

204 205 206 207 208
	// The send timeout includes the time required to connect
	// (although usually we will already have connected - we only need to
	// connect after a failed attempt to send)
	timeout := s.opts.SendTimeout - time.Since(start)
	if err = s.bsnet.msgToStream(ctx, stream, msg, timeout); err != nil {
209 210 211 212 213
		log.Infof("failed to send message to %s: %s", s.to, err)
		return err
	}

	return nil
dirkmc's avatar
dirkmc committed
214 215 216 217 218 219
}

func (bsnet *impl) Self() peer.ID {
	return bsnet.host.ID()
}

220 221 222 223 224 225 226 227 228 229 230
func (bsnet *impl) Ping(ctx context.Context, p peer.ID) ping.Result {
	ctx, cancel := context.WithCancel(ctx)
	defer cancel()
	res := <-ping.Ping(ctx, bsnet.host, p)
	return res
}

func (bsnet *impl) Latency(p peer.ID) time.Duration {
	return bsnet.host.Peerstore().LatencyEWMA(p)
}

dirkmc's avatar
dirkmc committed
231 232 233 234 235 236 237 238 239
// Indicates whether the given protocol supports HAVE / DONT_HAVE messages
func (bsnet *impl) SupportsHave(proto protocol.ID) bool {
	switch proto {
	case bsnet.protocolBitswapOneOne, bsnet.protocolBitswapOneZero, bsnet.protocolBitswapNoVers:
		return false
	}
	return true
}

240 241 242
func (bsnet *impl) msgToStream(ctx context.Context, s network.Stream, msg bsmsg.BitSwapMessage, timeout time.Duration) error {
	deadline := time.Now().Add(timeout)
	if dl, ok := ctx.Deadline(); ok && dl.Before(deadline) {
243 244
		deadline = dl
	}
Bob Potter's avatar
Bob Potter committed
245

246
	if err := s.SetWriteDeadline(deadline); err != nil {
247
		log.Warnf("error setting deadline: %s", err)
248 249
	}

dirkmc's avatar
dirkmc committed
250 251 252
	// Older Bitswap versions use a slightly different wire format so we need
	// to convert the message to the appropriate format depending on the remote
	// peer's Bitswap version.
253
	switch s.Protocol() {
dirkmc's avatar
dirkmc committed
254
	case bsnet.protocolBitswapOneOne, bsnet.protocolBitswap:
Bob Potter's avatar
Bob Potter committed
255
		if err := msg.ToNetV1(s); err != nil {
256 257 258
			log.Debugf("error: %s", err)
			return err
		}
dirkmc's avatar
dirkmc committed
259
	case bsnet.protocolBitswapOneZero, bsnet.protocolBitswapNoVers:
Bob Potter's avatar
Bob Potter committed
260
		if err := msg.ToNetV0(s); err != nil {
261 262 263 264 265 266
			log.Debugf("error: %s", err)
			return err
		}
	default:
		return fmt.Errorf("unrecognized protocol on remote: %s", s.Protocol())
	}
267

268 269
	atomic.AddUint64(&bsnet.stats.MessagesSent, 1)

270
	if err := s.SetWriteDeadline(time.Time{}); err != nil {
271
		log.Warnf("error resetting deadline: %s", err)
272
	}
273
	return nil
Jeromy's avatar
Jeromy committed
274 275
}

276
func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID, opts *MessageSenderOpts) (MessageSender, error) {
Dirk McCormick's avatar
Dirk McCormick committed
277
	opts = setDefaultOpts(opts)
278

279 280 281 282
	sender := &streamMessageSender{
		to:    p,
		bsnet: bsnet,
		opts:  opts,
Jeromy's avatar
Jeromy committed
283 284
	}

285 286
	err := sender.multiAttempt(ctx, func() error {
		_, err := sender.Connect(ctx)
287 288
		return err
	})
Jeromy's avatar
Jeromy committed
289 290 291 292 293

	if err != nil {
		return nil, err
	}

294
	return sender, nil
Jeromy's avatar
Jeromy committed
295 296
}

Dirk McCormick's avatar
Dirk McCormick committed
297 298 299 300 301 302 303 304 305 306 307 308
func setDefaultOpts(opts *MessageSenderOpts) *MessageSenderOpts {
	copy := *opts
	if opts.MaxRetries == 0 {
		copy.MaxRetries = 3
	}
	if opts.SendTimeout == 0 {
		copy.SendTimeout = sendMessageTimeout
	}
	if opts.SendErrorBackoff == 0 {
		copy.SendErrorBackoff = 100 * time.Millisecond
	}
	return &copy
309 310 311 312 313 314 315
}

func (bsnet *impl) SendMessage(
	ctx context.Context,
	p peer.ID,
	outgoing bsmsg.BitSwapMessage) error {

316 317 318 319
	tctx, cancel := context.WithTimeout(ctx, connectTimeout)
	defer cancel()

	s, err := bsnet.newStreamToPeer(tctx, p)
320 321 322
	if err != nil {
		return err
	}
323

324
	if err = bsnet.msgToStream(ctx, s, outgoing, sendMessageTimeout); err != nil {
Steven Allen's avatar
Steven Allen committed
325
		_ = s.Reset()
Steven Allen's avatar
Steven Allen committed
326
		return err
327
	}
328

329
	return s.Close()
330
}
331

332 333
func (bsnet *impl) newStreamToPeer(ctx context.Context, p peer.ID) (network.Stream, error) {
	return bsnet.host.NewStream(ctx, p, bsnet.supportedProtocols...)
334 335
}

336 337
func (bsnet *impl) SetDelegate(r Receiver) {
	bsnet.receiver = r
338
	bsnet.connectEvtMgr = newConnectEventManager(r)
dirkmc's avatar
dirkmc committed
339 340 341
	for _, proto := range bsnet.supportedProtocols {
		bsnet.host.SetStreamHandler(proto, bsnet.handleNewStream)
	}
hannahhoward's avatar
hannahhoward committed
342 343 344
	bsnet.host.Network().Notify((*netNotifiee)(bsnet))
	// TODO: StopNotify.

345
}
346

347
func (bsnet *impl) ConnectTo(ctx context.Context, p peer.ID) error {
Raúl Kripalani's avatar
Raúl Kripalani committed
348
	return bsnet.host.Connect(ctx, peer.AddrInfo{ID: p})
349 350
}

dirkmc's avatar
dirkmc committed
351 352 353 354
func (bsnet *impl) DisconnectFrom(ctx context.Context, p peer.ID) error {
	panic("Not implemented: DisconnectFrom() is only used by tests")
}

355
// FindProvidersAsync returns a channel of providers for the given key.
356
func (bsnet *impl) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.ID {
357
	out := make(chan peer.ID, max)
358 359
	go func() {
		defer close(out)
360
		providers := bsnet.routing.FindProvidersAsync(ctx, k, max)
361
		for info := range providers {
362 363
			if info.ID == bsnet.host.ID() {
				continue // ignore self as provider
364
			}
Raúl Kripalani's avatar
Raúl Kripalani committed
365
			bsnet.host.Peerstore().AddAddrs(info.ID, info.Addrs, peerstore.TempAddrTTL)
366 367
			select {
			case <-ctx.Done():
368
				return
369 370 371 372 373
			case out <- info.ID:
			}
		}
	}()
	return out
374 375 376
}

// Provide provides the key to the network
377
func (bsnet *impl) Provide(ctx context.Context, k cid.Cid) error {
378
	return bsnet.routing.Provide(ctx, k, true)
379 380
}

381
// handleNewStream receives a new stream from the network.
Raúl Kripalani's avatar
Raúl Kripalani committed
382
func (bsnet *impl) handleNewStream(s network.Stream) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
383
	defer s.Close()
384 385

	if bsnet.receiver == nil {
Steven Allen's avatar
Steven Allen committed
386
		_ = s.Reset()
387 388 389
		return
	}

Steven Allen's avatar
Steven Allen committed
390
	reader := msgio.NewVarintReaderSize(s, network.MessageSizeMax)
391
	for {
Steven Allen's avatar
Steven Allen committed
392
		received, err := bsmsg.FromMsgReader(reader)
393
		if err != nil {
Jeromy's avatar
Jeromy committed
394
			if err != io.EOF {
Steven Allen's avatar
Steven Allen committed
395
				_ = s.Reset()
396
				bsnet.receiver.ReceiveError(err)
Jeromy's avatar
Jeromy committed
397 398
				log.Debugf("bitswap net handleNewStream from %s error: %s", s.Conn().RemotePeer(), err)
			}
399 400
			return
		}
401

402 403 404
		p := s.Conn().RemotePeer()
		ctx := context.Background()
		log.Debugf("bitswap net handleNewStream from %s", s.Conn().RemotePeer())
405
		bsnet.connectEvtMgr.OnMessage(s.Conn().RemotePeer())
406
		bsnet.receiver.ReceiveMessage(ctx, p, received)
407
		atomic.AddUint64(&bsnet.stats.MessagesRecvd, 1)
408
	}
409
}
410

Raúl Kripalani's avatar
Raúl Kripalani committed
411
func (bsnet *impl) ConnectionManager() connmgr.ConnManager {
412 413 414
	return bsnet.host.ConnManager()
}

415 416
func (bsnet *impl) Stats() Stats {
	return Stats{
417 418 419 420 421
		MessagesRecvd: atomic.LoadUint64(&bsnet.stats.MessagesRecvd),
		MessagesSent:  atomic.LoadUint64(&bsnet.stats.MessagesSent),
	}
}

422 423 424 425 426 427
type netNotifiee impl

func (nn *netNotifiee) impl() *impl {
	return (*impl)(nn)
}

Raúl Kripalani's avatar
Raúl Kripalani committed
428
func (nn *netNotifiee) Connected(n network.Network, v network.Conn) {
vyzo's avatar
vyzo committed
429 430 431 432 433
	// ignore transient connections
	if v.Stat().Transient {
		return
	}

434
	nn.impl().connectEvtMgr.Connected(v.RemotePeer())
435
}
Raúl Kripalani's avatar
Raúl Kripalani committed
436
func (nn *netNotifiee) Disconnected(n network.Network, v network.Conn) {
vyzo's avatar
vyzo committed
437 438 439 440 441
	// ignore transient connections
	if v.Stat().Transient {
		return
	}

442
	nn.impl().connectEvtMgr.Disconnected(v.RemotePeer())
443
}
dirkmc's avatar
dirkmc committed
444
func (nn *netNotifiee) OpenedStream(n network.Network, s network.Stream) {}
Raúl Kripalani's avatar
Raúl Kripalani committed
445 446 447
func (nn *netNotifiee) ClosedStream(n network.Network, v network.Stream) {}
func (nn *netNotifiee) Listen(n network.Network, a ma.Multiaddr)         {}
func (nn *netNotifiee) ListenClose(n network.Network, a ma.Multiaddr)    {}