github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/netpoll_solaris.go (about) 1 // Copyright 2014 The Go 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 5 package runtime 6 7 import ( 8 "internal/goarch" 9 "runtime/internal/atomic" 10 "unsafe" 11 ) 12 13 // Solaris runtime-integrated network poller. 14 // 15 // Solaris uses event ports for scalable network I/O. Event 16 // ports are level-triggered, unlike epoll and kqueue which 17 // can be configured in both level-triggered and edge-triggered 18 // mode. Level triggering means we have to keep track of a few things 19 // ourselves. After we receive an event for a file descriptor, 20 // it's our responsibility to ask again to be notified for future 21 // events for that descriptor. When doing this we must keep track of 22 // what kind of events the goroutines are currently interested in, 23 // for example a fd may be open both for reading and writing. 24 // 25 // A description of the high level operation of this code 26 // follows. Networking code will get a file descriptor by some means 27 // and will register it with the netpolling mechanism by a code path 28 // that eventually calls runtime·netpollopen. runtime·netpollopen 29 // calls port_associate with an empty event set. That means that we 30 // will not receive any events at this point. The association needs 31 // to be done at this early point because we need to process the I/O 32 // readiness notification at some point in the future. If I/O becomes 33 // ready when nobody is listening, when we finally care about it, 34 // nobody will tell us anymore. 35 // 36 // Beside calling runtime·netpollopen, the networking code paths 37 // will call runtime·netpollarm each time goroutines are interested 38 // in doing network I/O. Because now we know what kind of I/O we 39 // are interested in (reading/writing), we can call port_associate 40 // passing the correct type of event set (POLLIN/POLLOUT). As we made 41 // sure to have already associated the file descriptor with the port, 42 // when we now call port_associate, we will unblock the main poller 43 // loop (in runtime·netpoll) right away if the socket is actually 44 // ready for I/O. 45 // 46 // The main poller loop runs in its own thread waiting for events 47 // using port_getn. When an event happens, it will tell the scheduler 48 // about it using runtime·netpollready. Besides doing this, it must 49 // also re-associate the events that were not part of this current 50 // notification with the file descriptor. Failing to do this would 51 // mean each notification will prevent concurrent code using the 52 // same file descriptor in parallel. 53 // 54 // The logic dealing with re-associations is encapsulated in 55 // runtime·netpollupdate. This function takes care to associate the 56 // descriptor only with the subset of events that were previously 57 // part of the association, except the one that just happened. We 58 // can't re-associate with that right away, because event ports 59 // are level triggered so it would cause a busy loop. Instead, that 60 // association is effected only by the runtime·netpollarm code path, 61 // when Go code actually asks for I/O. 62 // 63 // The open and arming mechanisms are serialized using the lock 64 // inside PollDesc. This is required because the netpoll loop runs 65 // asynchronously in respect to other Go code and by the time we get 66 // to call port_associate to update the association in the loop, the 67 // file descriptor might have been closed and reopened already. The 68 // lock allows runtime·netpollupdate to be called synchronously from 69 // the loop thread while preventing other threads operating to the 70 // same PollDesc, so once we unblock in the main loop, until we loop 71 // again we know for sure we are always talking about the same file 72 // descriptor and can safely access the data we want (the event set). 73 74 //go:cgo_import_dynamic libc_port_create port_create "libc.so" 75 //go:cgo_import_dynamic libc_port_associate port_associate "libc.so" 76 //go:cgo_import_dynamic libc_port_dissociate port_dissociate "libc.so" 77 //go:cgo_import_dynamic libc_port_getn port_getn "libc.so" 78 //go:cgo_import_dynamic libc_port_alert port_alert "libc.so" 79 80 //go:linkname libc_port_create libc_port_create 81 //go:linkname libc_port_associate libc_port_associate 82 //go:linkname libc_port_dissociate libc_port_dissociate 83 //go:linkname libc_port_getn libc_port_getn 84 //go:linkname libc_port_alert libc_port_alert 85 86 var ( 87 libc_port_create, 88 libc_port_associate, 89 libc_port_dissociate, 90 libc_port_getn, 91 libc_port_alert libcFunc 92 netpollWakeSig atomic.Uint32 // used to avoid duplicate calls of netpollBreak 93 ) 94 95 func errno() int32 { 96 return *getg().m.perrno 97 } 98 99 func fcntl(fd, cmd, arg int32) int32 { 100 return int32(sysvicall3(&libc_fcntl, uintptr(fd), uintptr(cmd), uintptr(arg))) 101 } 102 103 func port_create() int32 { 104 return int32(sysvicall0(&libc_port_create)) 105 } 106 107 func port_associate(port, source int32, object uintptr, events uint32, user uintptr) int32 { 108 return int32(sysvicall5(&libc_port_associate, uintptr(port), uintptr(source), object, uintptr(events), user)) 109 } 110 111 func port_dissociate(port, source int32, object uintptr) int32 { 112 return int32(sysvicall3(&libc_port_dissociate, uintptr(port), uintptr(source), object)) 113 } 114 115 func port_getn(port int32, evs *portevent, max uint32, nget *uint32, timeout *timespec) int32 { 116 return int32(sysvicall5(&libc_port_getn, uintptr(port), uintptr(unsafe.Pointer(evs)), uintptr(max), uintptr(unsafe.Pointer(nget)), uintptr(unsafe.Pointer(timeout)))) 117 } 118 119 func port_alert(port int32, flags, events uint32, user uintptr) int32 { 120 return int32(sysvicall4(&libc_port_alert, uintptr(port), uintptr(flags), uintptr(events), user)) 121 } 122 123 var portfd int32 = -1 124 125 func netpollinit() { 126 portfd = port_create() 127 if portfd >= 0 { 128 fcntl(portfd, _F_SETFD, _FD_CLOEXEC) 129 return 130 } 131 132 print("runtime: port_create failed (errno=", errno(), ")\n") 133 throw("runtime: netpollinit failed") 134 } 135 136 func netpollIsPollDescriptor(fd uintptr) bool { 137 return fd == uintptr(portfd) 138 } 139 140 func netpollopen(fd uintptr, pd *pollDesc) int32 { 141 lock(&pd.lock) 142 // We don't register for any specific type of events yet, that's 143 // netpollarm's job. We merely ensure we call port_associate before 144 // asynchronous connect/accept completes, so when we actually want 145 // to do any I/O, the call to port_associate (from netpollarm, 146 // with the interested event set) will unblock port_getn right away 147 // because of the I/O readiness notification. 148 pd.user = 0 149 tp := taggedPointerPack(unsafe.Pointer(pd), pd.fdseq.Load()) 150 // Note that this won't work on a 32-bit system, 151 // as taggedPointer is always 64-bits but uintptr will be 32 bits. 152 // Fortunately we only support Solaris on amd64. 153 if goarch.PtrSize != 8 { 154 throw("runtime: netpollopen: unsupported pointer size") 155 } 156 r := port_associate(portfd, _PORT_SOURCE_FD, fd, 0, uintptr(tp)) 157 unlock(&pd.lock) 158 return r 159 } 160 161 func netpollclose(fd uintptr) int32 { 162 return port_dissociate(portfd, _PORT_SOURCE_FD, fd) 163 } 164 165 // Updates the association with a new set of interested events. After 166 // this call, port_getn will return one and only one event for that 167 // particular descriptor, so this function needs to be called again. 168 func netpollupdate(pd *pollDesc, set, clear uint32) { 169 if pd.info().closing() { 170 return 171 } 172 173 old := pd.user 174 events := (old & ^clear) | set 175 if old == events { 176 return 177 } 178 179 tp := taggedPointerPack(unsafe.Pointer(pd), pd.fdseq.Load()) 180 if events != 0 && port_associate(portfd, _PORT_SOURCE_FD, pd.fd, events, uintptr(tp)) != 0 { 181 print("runtime: port_associate failed (errno=", errno(), ")\n") 182 throw("runtime: netpollupdate failed") 183 } 184 pd.user = events 185 } 186 187 // subscribe the fd to the port such that port_getn will return one event. 188 func netpollarm(pd *pollDesc, mode int) { 189 lock(&pd.lock) 190 switch mode { 191 case 'r': 192 netpollupdate(pd, _POLLIN, 0) 193 case 'w': 194 netpollupdate(pd, _POLLOUT, 0) 195 default: 196 throw("runtime: bad mode") 197 } 198 unlock(&pd.lock) 199 } 200 201 // netpollBreak interrupts a port_getn wait. 202 func netpollBreak() { 203 // Failing to cas indicates there is an in-flight wakeup, so we're done here. 204 if !netpollWakeSig.CompareAndSwap(0, 1) { 205 return 206 } 207 208 // Use port_alert to put portfd into alert mode. 209 // This will wake up all threads sleeping in port_getn on portfd, 210 // and cause their calls to port_getn to return immediately. 211 // Further, until portfd is taken out of alert mode, 212 // all calls to port_getn will return immediately. 213 if port_alert(portfd, _PORT_ALERT_UPDATE, _POLLHUP, uintptr(unsafe.Pointer(&portfd))) < 0 { 214 if e := errno(); e != _EBUSY { 215 println("runtime: port_alert failed with", e) 216 throw("runtime: netpoll: port_alert failed") 217 } 218 } 219 } 220 221 // netpoll checks for ready network connections. 222 // Returns list of goroutines that become runnable. 223 // delay < 0: blocks indefinitely 224 // delay == 0: does not block, just polls 225 // delay > 0: block for up to that many nanoseconds 226 func netpoll(delay int64) gList { 227 if portfd == -1 { 228 return gList{} 229 } 230 231 var wait *timespec 232 var ts timespec 233 if delay < 0 { 234 wait = nil 235 } else if delay == 0 { 236 wait = &ts 237 } else { 238 ts.setNsec(delay) 239 if ts.tv_sec > 1e6 { 240 // An arbitrary cap on how long to wait for a timer. 241 // 1e6 s == ~11.5 days. 242 ts.tv_sec = 1e6 243 } 244 wait = &ts 245 } 246 247 var events [128]portevent 248 retry: 249 var n uint32 = 1 250 r := port_getn(portfd, &events[0], uint32(len(events)), &n, wait) 251 e := errno() 252 if r < 0 && e == _ETIME && n > 0 { 253 // As per port_getn(3C), an ETIME failure does not preclude the 254 // delivery of some number of events. Treat a timeout failure 255 // with delivered events as a success. 256 r = 0 257 } 258 if r < 0 { 259 if e != _EINTR && e != _ETIME { 260 print("runtime: port_getn on fd ", portfd, " failed (errno=", e, ")\n") 261 throw("runtime: netpoll failed") 262 } 263 // If a timed sleep was interrupted and there are no events, 264 // just return to recalculate how long we should sleep now. 265 if delay > 0 { 266 return gList{} 267 } 268 goto retry 269 } 270 271 var toRun gList 272 for i := 0; i < int(n); i++ { 273 ev := &events[i] 274 275 if ev.portev_source == _PORT_SOURCE_ALERT { 276 if ev.portev_events != _POLLHUP || unsafe.Pointer(ev.portev_user) != unsafe.Pointer(&portfd) { 277 throw("runtime: netpoll: bad port_alert wakeup") 278 } 279 if delay != 0 { 280 // Now that a blocking call to netpoll 281 // has seen the alert, take portfd 282 // back out of alert mode. 283 // See the comment in netpollBreak. 284 if port_alert(portfd, 0, 0, 0) < 0 { 285 e := errno() 286 println("runtime: port_alert failed with", e) 287 throw("runtime: netpoll: port_alert failed") 288 } 289 netpollWakeSig.Store(0) 290 } 291 continue 292 } 293 294 if ev.portev_events == 0 { 295 continue 296 } 297 298 tp := taggedPointer(uintptr(unsafe.Pointer(ev.portev_user))) 299 pd := (*pollDesc)(tp.pointer()) 300 if pd.fdseq.Load() != tp.tag() { 301 continue 302 } 303 304 var mode, clear int32 305 if (ev.portev_events & (_POLLIN | _POLLHUP | _POLLERR)) != 0 { 306 mode += 'r' 307 clear |= _POLLIN 308 } 309 if (ev.portev_events & (_POLLOUT | _POLLHUP | _POLLERR)) != 0 { 310 mode += 'w' 311 clear |= _POLLOUT 312 } 313 // To effect edge-triggered events, we need to be sure to 314 // update our association with whatever events were not 315 // set with the event. For example if we are registered 316 // for POLLIN|POLLOUT, and we get POLLIN, besides waking 317 // the goroutine interested in POLLIN we have to not forget 318 // about the one interested in POLLOUT. 319 if clear != 0 { 320 lock(&pd.lock) 321 netpollupdate(pd, 0, uint32(clear)) 322 unlock(&pd.lock) 323 } 324 325 if mode != 0 { 326 // TODO(mikio): Consider implementing event 327 // scanning error reporting once we are sure 328 // about the event port on SmartOS. 329 // 330 // See golang.org/x/issue/30840. 331 netpollready(&toRun, pd, mode) 332 } 333 } 334 335 return toRun 336 }