-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for native guest agent connection for each driver
Signed-off-by: Balaji Vijayakumar <kuttibalaji.v6@gmail.com>
- Loading branch information
1 parent
79cfe42
commit a6dca6a
Showing
19 changed files
with
289 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package serialport | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
type SerialConn struct { | ||
serialDevice string | ||
port *Port | ||
} | ||
|
||
var _ net.Conn = (*SerialConn)(nil) | ||
|
||
func DialSerial(serialDevice string) (*SerialConn, error) { | ||
s, err := openPort(serialDevice) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &SerialConn{port: s, serialDevice: serialDevice}, nil | ||
} | ||
|
||
func (c *SerialConn) Read(b []byte) (n int, err error) { | ||
return c.port.Read(b) | ||
} | ||
|
||
func (c *SerialConn) Write(b []byte) (n int, err error) { | ||
return c.port.Write(b) | ||
} | ||
|
||
func (c *SerialConn) Close() error { | ||
// There is no need to close the serial port every time. | ||
// So just do nothing. | ||
return nil | ||
} | ||
|
||
func (c *SerialConn) LocalAddr() net.Addr { | ||
return &net.UnixAddr{Name: "virtio-port:" + c.serialDevice, Net: "virtio"} | ||
} | ||
|
||
func (c *SerialConn) RemoteAddr() net.Addr { | ||
return &net.UnixAddr{Name: "qemu-host", Net: "virtio"} | ||
} | ||
|
||
func (c *SerialConn) SetDeadline(_ time.Time) error { | ||
return nil | ||
} | ||
|
||
func (c *SerialConn) SetReadDeadline(_ time.Time) error { | ||
return nil | ||
} | ||
|
||
func (c *SerialConn) SetWriteDeadline(_ time.Time) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package serialport | ||
|
||
import ( | ||
"net" | ||
"sync" | ||
"syscall" | ||
|
||
"golang.org/x/net/netutil" | ||
) | ||
|
||
type SerialListener struct { | ||
mu sync.Mutex | ||
conn *SerialConn | ||
closed bool | ||
} | ||
|
||
func Listen(serialDevice string) (net.Listener, error) { | ||
c, err := DialSerial(serialDevice) | ||
if err != nil { | ||
return nil, &net.OpError{Op: "dial", Net: "virtio", Source: c.LocalAddr(), Addr: nil, Err: err} | ||
} | ||
|
||
return netutil.LimitListener(&SerialListener{conn: c}, 1), nil | ||
} | ||
|
||
func (ln *SerialListener) ok() bool { | ||
return ln != nil && ln.conn != nil && !ln.closed | ||
} | ||
|
||
func (ln *SerialListener) Accept() (net.Conn, error) { | ||
ln.mu.Lock() | ||
defer ln.mu.Unlock() | ||
|
||
if !ln.ok() { | ||
return nil, syscall.EINVAL | ||
} | ||
|
||
return ln.conn, nil | ||
} | ||
|
||
func (ln *SerialListener) Close() error { | ||
ln.mu.Lock() | ||
defer ln.mu.Unlock() | ||
|
||
if !ln.ok() { | ||
return syscall.EINVAL | ||
} | ||
|
||
if ln.closed { | ||
return nil | ||
} | ||
ln.closed = true | ||
|
||
return nil | ||
} | ||
|
||
func (ln *SerialListener) Addr() net.Addr { | ||
if ln.ok() { | ||
return ln.conn.LocalAddr() | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package serialport | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func openPort(name string) (p *Port, err error) { | ||
f, err := os.OpenFile(name, unix.O_RDWR|unix.O_NOCTTY|unix.O_NONBLOCK, 0o666) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func() { | ||
if err != nil && f != nil { | ||
f.Close() | ||
} | ||
}() | ||
|
||
fd := f.Fd() | ||
if err = unix.SetNonblock(int(fd), false); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Port{f: f}, nil | ||
} | ||
|
||
type Port struct { | ||
f *os.File | ||
} | ||
|
||
func (p *Port) Read(b []byte) (n int, err error) { | ||
return p.f.Read(b) | ||
} | ||
|
||
func (p *Port) Write(b []byte) (n int, err error) { | ||
return p.f.Write(b) | ||
} | ||
|
||
func (p *Port) Close() (err error) { | ||
return p.f.Close() | ||
} |
Oops, something went wrong.