Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | package vif |
| 6 | |
| 7 | import ( |
| 8 | "math/rand" |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 9 | "net" |
Asim Shankar | 0cd7353 | 2014-08-25 11:01:29 -0700 | [diff] [blame] | 10 | "runtime" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 11 | "sync" |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 12 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 13 | "v.io/v23/rpc" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // Set implements a set of VIFs keyed by (network, address) of the underlying |
| 17 | // connection. Multiple goroutines can invoke methods on the Set |
| 18 | // simultaneously. |
| 19 | type Set struct { |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 20 | mu sync.RWMutex |
| 21 | set map[string][]*VIF // GUARDED_BY(mu) |
| 22 | started map[string]bool // GUARDED_BY(mu) |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 23 | keys map[*VIF]string // GUARDED_BY(mu) |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 24 | cond *sync.Cond |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // NewSet returns a new Set of VIFs. |
| 28 | func NewSet() *Set { |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 29 | s := &Set{ |
| 30 | set: make(map[string][]*VIF), |
| 31 | started: make(map[string]bool), |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 32 | keys: make(map[*VIF]string), |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 33 | } |
| 34 | s.cond = sync.NewCond(&s.mu) |
| 35 | return s |
| 36 | } |
| 37 | |
| 38 | // BlockingFind returns a VIF where the remote end of the underlying network connection |
| 39 | // is identified by the provided (network, address). Returns nil if there is no |
| 40 | // such VIF. |
| 41 | // |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 42 | // The caller is required to call the returned unblock function, to avoid deadlock. |
| 43 | // Until the returned function is called, all new BlockingFind calls for this |
| 44 | // network and address will block. |
| 45 | func (s *Set) BlockingFind(network, address string) (*VIF, func()) { |
| 46 | if isNonDistinctConn(network, address) { |
| 47 | return nil, func() {} |
| 48 | } |
| 49 | |
| 50 | k := key(network, address) |
| 51 | |
| 52 | s.mu.Lock() |
| 53 | defer s.mu.Unlock() |
| 54 | |
| 55 | for s.started[k] { |
| 56 | s.cond.Wait() |
| 57 | } |
| 58 | |
| 59 | _, _, _, p := rpc.RegisteredProtocol(network) |
| 60 | for _, n := range p { |
| 61 | if vifs := s.set[key(n, address)]; len(vifs) > 0 { |
| 62 | return vifs[rand.Intn(len(vifs))], func() {} |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | s.started[k] = true |
| 67 | return nil, func() { s.unblock(network, address) } |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 70 | // unblock marks the status of the network, address as no longer started, and |
Suharsh Sivakumar | 04e0e28 | 2015-05-02 23:24:04 -0700 | [diff] [blame] | 71 | // broadcasts waiting threads. |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 72 | func (s *Set) unblock(network, address string) { |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 73 | s.mu.Lock() |
| 74 | delete(s.started, key(network, address)) |
| 75 | s.cond.Broadcast() |
| 76 | s.mu.Unlock() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Suharsh Sivakumar | 04e0e28 | 2015-05-02 23:24:04 -0700 | [diff] [blame] | 79 | // Insert adds a VIF to the set. |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 80 | func (s *Set) Insert(vif *VIF, network, address string) { |
| 81 | k := key(network, address) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 82 | s.mu.Lock() |
| 83 | defer s.mu.Unlock() |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 84 | s.keys[vif] = k |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 85 | vifs := s.set[k] |
| 86 | for _, v := range vifs { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | if v == vif { |
| 88 | return |
| 89 | } |
| 90 | } |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 91 | s.set[k] = append(vifs, vif) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Suharsh Sivakumar | 04e0e28 | 2015-05-02 23:24:04 -0700 | [diff] [blame] | 94 | // Delete removes a VIF from the set. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 95 | func (s *Set) Delete(vif *VIF) { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 96 | s.mu.Lock() |
| 97 | defer s.mu.Unlock() |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 98 | k := s.keys[vif] |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 99 | vifs := s.set[k] |
| 100 | for i, v := range vifs { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 101 | if v == vif { |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 102 | if len(vifs) == 1 { |
| 103 | delete(s.set, k) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 104 | } else { |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 105 | s.set[k] = append(vifs[:i], vifs[i+1:]...) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 106 | } |
Suharsh Sivakumar | 7e93ce5 | 2015-05-07 17:46:13 -0700 | [diff] [blame] | 107 | delete(s.keys, vif) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 108 | return |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // List returns the elements in the set as a slice. |
| 114 | func (s *Set) List() []*VIF { |
| 115 | s.mu.RLock() |
| 116 | defer s.mu.RUnlock() |
| 117 | l := make([]*VIF, 0, len(s.set)) |
| 118 | for _, vifs := range s.set { |
| 119 | l = append(l, vifs...) |
| 120 | } |
| 121 | return l |
| 122 | } |
| 123 | |
Jungho Ahn | 9a5b307 | 2015-02-24 13:59:25 -0800 | [diff] [blame] | 124 | func key(network, address string) string { |
| 125 | if network == "tcp" || network == "ws" { |
| 126 | host, _, _ := net.SplitHostPort(address) |
| 127 | switch ip := net.ParseIP(host); { |
| 128 | case ip == nil: |
| 129 | // This may happen when address is a hostname. But we do not care |
| 130 | // about it, since vif cannot be found with a hostname anyway. |
| 131 | case ip.To4() != nil: |
| 132 | network += "4" |
| 133 | default: |
| 134 | network += "6" |
| 135 | } |
| 136 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 137 | return network + ":" + address |
| 138 | } |
Suharsh Sivakumar | 859ea0f | 2015-04-29 23:51:39 -0700 | [diff] [blame] | 139 | |
| 140 | // Some network connections (like those created with net.Pipe or Unix sockets) |
| 141 | // do not end up with distinct net.Addrs on distinct net.Conns. |
| 142 | func isNonDistinctConn(network, address string) bool { |
| 143 | return len(address) == 0 || |
| 144 | (network == "pipe" && address == "pipe") || |
| 145 | (runtime.GOOS == "linux" && network == "unix" && address == "@") |
| 146 | } |