[go-nuts] Imp.ListenPacket fail for IPv6 address

2019-10-09 Thread 'Serguei Bezverkhi (sbezverk)' via golang-nuts
Hello,

Here is the part of code where I attempt to listen for icmp packet in a 
namespace. The same code is used for IPv4 and IPv6, only proto and scr are 
differ depending on ipv4 or ipv6 mode.
For IPv4 address code work as expected but for IPv6 ListenPacket fails.

org, err := netns.Get()
if err != nil {
return err
}
defer netns.Set(org)
if err := netns.Set(sourceNS); err != nil {
return err
}

c, err := icmp.ListenPacket(proto, src)
if err != nil {
return fmt.Errorf("call ListenPacket failed 
with error: %+v", err)
}
defer c.Close()

ListenPacket fails with: “call ListenPacket failed with error: listen 
ip6:ipv6-icmp 2001:1::1: bind: cannot assign requested address”

Here is how proto and src looks like for IPv6:

Proto: ip6:ipv6-icmp Address: 2001:1::1

I confirmed that namespace has veth interface with right ipv6 address.

Links found in the namespace, as you can see ipv6 address I am trying to bind 
ListenPacket is present.

Link: lo
Link: veth-1-612de81e
- 2001:1::1

Appreciate some ideas what I do wrong for ipv6 case.
Thank you
Serguei

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/1DA7A4FE-65FA-46B4-BC58-5AB5BDB080A2%40cisco.com.


Re: [go-nuts] Re: Ioctl Syscall with go and c

2018-08-26 Thread 'Serguei Bezverkhi (sbezverk)' via golang-nuts
Hi Jake,

Ioctl implementation in unix package seems a bit limited, how do you pass for 
example a required struct if “func IoctlSetInt(fd int, req uint, value int)” 
allows only to accept int?? If this package had a func accepting uintptr as a 
parameter, then I guess that could work.

Thank you
Serguei


From:  on behalf of "jake6...@gmail.com" 

Date: Friday, August 24, 2018 at 12:09 PM
To: golang-nuts 
Subject: [go-nuts] Re: Ioctl Syscall with go and c

Probably unrelated to your problem, but IIUC, the syscall package is 
"deprecated", or "frozen". According to the syscall 
documentation:
This package is locked down. Code outside the standard Go repository should be 
migrated to use the corresponding package in the golang.org/x/sys repository. 
That is also where updates required by new systems or versions should be 
applied.

So, while unlikely, it is possible that is the problem. In any case, unless I 
misunderstand, the preferred package for you to use is golang.org/x/sys/unix.

On Thursday, August 23, 2018 at 7:02:13 AM UTC-4, sbez...@cisco.com wrote:
Hello,

I am converting some C code to Go and hit an issue with one particular Syscall:

In C:

device = ioctl(group, 0x3b6a, path);
where path is char[N]

In Go:
ioctlId := 0x3b6a
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer()),
uintptr(unsafe.Pointer(pciDevice)),
)
Where pciDevice is *string with exactly the same value as path in C.

When I run Go bits on the same h/w, same OS, same everything, it fails with 
"errno 22 (invalid argument)". It seems that the issue is how string gets 
passed to Syscall, but I could not find any examples how to do it correctly.
Appreciate some advice here.

Thank you
Serguei
--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: Ioctl Syscall with go and c

2018-08-23 Thread 'Serguei Bezverkhi (sbezverk)' via golang-nuts
I copied the whole thing to https://github.com/sbezverk/vfio

Running it without actual vfio/iommu might be problematic though.

Before this specific ioctl can be run 2 open calls must succeed. See func main.

Thank you for looking into this problem
Serguei

Sent from my iPhone

> On Aug 23, 2018, at 12:49 PM, Tamás Gulácsi  wrote:
> 
> Cany you give a complete example?
> 
>  https://play.golang.org/p/ZQSf-PwMtd9
> 
> says
> 
>  "device=18446744073709551615 errno=inappropriate ioctl for device"
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Re: Ioctl Syscall with go and c

2018-08-23 Thread 'Serguei Bezverkhi (sbezverk)' via golang-nuts
Hello Jake,

Apologies about formatting, here is correctly formatted code:

// GetGroupFD gets File descriptor for a specified by PCI address device
func GetGroupFD(group int, pciDevice string) (int, error) {
buffer := make([]byte, len(pciDevice)+1)
for i, c := range pciDevice {
buffer[i] = uint8(c)
}
ioctlID := VFIO_GROUP_GET_DEVICE_FD()
fmt.Printf("VFIO_GROUP_GET_DEVICE_FD() returned: %04x\n", 
ioctlID)
buffer[len(pciDevice)] = 0x0
fmt.Printf("pciDevice: %s\n", string(buffer))
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer()),
uintptr(unsafe.Pointer([0])),
)
if errno != 0 {
return 0, fmt.Errorf("fail to get file 
descriptor for %d with errno: %+v", group, errno)
}
return int(device), nil
}

I have also addressed your suggestions without any success. It is latest Centos 
7.5.

Thank you
Serguei

From:  on behalf of "jake6...@gmail.com" 

Date: Thursday, August 23, 2018 at 12:27 PM
To: golang-nuts 
Subject: Re: [go-nuts] Re: Ioctl Syscall with go and c

First off, I don't know how you are posting your code samples, but they are 
unreadable in my Firefox and Chrome on windows.

Second, I would point out that the system you use for converting a "string" to 
the buffer will fail if there are any non-ascii characters.

Third, OT, but I wonder why pciDevice is a string pointer, instead of a string?

Personally, I don't see anything decisively incorrect, but there is a lot of 
context that is missing. If you wanted to indicate your OS, and provide a 
complete working sample, I'm sure I, or someone else could help more.

One thing that jumps out at me is the use of  
uintptr(unsafe.Pointer()), in your initial example. It is hidden by the 
function  VFIO_GROUP_GET_DEVICE_FD(), which is not provided, in your larger 
example. Are you sure you want the pointer to that value, and not the actual 
value? Maybe uintptr(ioctlId)) instead. Seems more standard for IOCTL to me.

And, of course the changes you made to create an appropriate string buffer are 
necessary.


On Thursday, August 23, 2018 at 11:47:18 AM UTC-4, sbez...@cisco.com wrote:
Hi Max,

Thanks for the suggestion, unfortunately it did not help, see below:

func GetGroupFD(group int, pciDevice *string) (int, error) {
fmt.Printf("VFIO_GROUP_GET_DEVICE_FD() returned: %04x\n", 
VFIO_GROUP_GET_DEVICE_FD())
buffer := make([]byte, len(*pciDevice)+1)
for i, c := range *pciDevice {
buffer[i] = uint8(c)
}
buffer[len(*pciDevice)] = 0x0
fmt.Printf("pciDevice: %s\n", string(buffer))
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(VFIO_GROUP_GET_DEVICE_FD()),
uintptr(unsafe.Pointer([0])),
)
if errno != 0 {
return 0, fmt.Errorf("fail to get file descriptor for %d with errno: 
%+v", group, errno)
}
return int(device), nil
}

Any other suggestions?
Thank you
Serguei

From: > on behalf of Max 
>
Date: Thursday, August 23, 2018 at 10:46 AM
To: golang-nuts >
Subject: Re: [go-nuts] Re: Ioctl Syscall with go and c

Hi Serguei,

a Go string or *string do not correspond to a C char *
You must pass the address of the first byte of a sufficiently large buffer:

func GetGroupFD(group int, pciDevice *string) (int, error) {
  const N = 256
  var buffer [N]byte
  device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer()),
[0],
)
   /* if ioctl() is successful, find '\0' in buffer[] and copy the relevant 
portion in *pciDevice */
}

On Thursday, August 23, 2018 at 2:55:29 PM UTC+2, sbez...@cisco.com wrote:
I changed code a little bit to be able to examine variables:

 func GetGroupFD(group int, pciDevice *string) (int, error) {
ioctlId := 0x3b6a
var buffer uintptr
buffer = uintptr(unsafe.Pointer(pciDevice))

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving 

Re: [go-nuts] Re: Ioctl Syscall with go and c

2018-08-23 Thread 'Serguei Bezverkhi (sbezverk)' via golang-nuts
I changed code a little bit to be able to examine variables:

 func GetGroupFD(group int, pciDevice *string) (int, error) {
ioctlId := 0x3b6a
var buffer uintptr
buffer = uintptr(unsafe.Pointer(pciDevice))
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer()),
buffer,
)

(gdb) p 
$8 = (uintptr *) 0xc4200c7b88
(gdb) x/g 0xc4200c7b88
0xc4200c7b88:   0x00c4200c7d08
(gdb) x/g 0x00c4200c7d08
0xc4200c7d08:   0x00c4200122d0
(gdb) x/g 0x00c4200122d0
0xc4200122d0:   0x3a31383a30303030

0x30303030 is start of expected string “:81:11.1”,  first 4 bytes are not 
part of it. Anybody knows where those 4 bytes could have come from? And how to 
get rid of them?
Thank you
Serguei

From:  on behalf of Tamás Gulácsi 

Date: Thursday, August 23, 2018 at 8:13 AM
To: golang-nuts 
Subject: [go-nuts] Re: Ioctl Syscall with go and c

Is it \0-terminated?

2018. augusztus 23., csütörtök 13:02:13 UTC+2 időpontban sbez...@cisco.com a 
következőt írta:
Hello,

I am converting some C code to Go and hit an issue with one particular Syscall:

In C:

device = ioctl(group, 0x3b6a, path);
where path is char[N]

In Go:
ioctlId := 0x3b6a
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer()),
uintptr(unsafe.Pointer(pciDevice)),
)
Where pciDevice is *string with exactly the same value as path in C.

When I run Go bits on the same h/w, same OS, same everything, it fails with 
"errno 22 (invalid argument)". It seems that the issue is how string gets 
passed to Syscall, but I could not find any examples how to do it correctly.
Appreciate some advice here.

Thank you
Serguei
--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Ioctl Syscall with go and c

2018-08-23 Thread 'Serguei Bezverkhi (sbezverk)' via golang-nuts
Hello,

I am converting some C code to Go and hit an issue with one particular Syscall:

In C:

device = ioctl(group, 0x3b6a, path);
where path is char[N]

In Go:
ioctlId := 0x3b6a
device, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(group),
uintptr(unsafe.Pointer()),
uintptr(unsafe.Pointer(pciDevice)),
)
Where pciDevice is *string with exactly the same value as path in C.

When I run Go bits on the same h/w, same OS, same everything, it fails with 
"errno 22 (invalid argument)". It seems that the issue is how string gets 
passed to Syscall, but I could not find any examples how to do it correctly.
Appreciate some advice here.

Thank you
Serguei

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.