Learning the list SKB is on - linux-kernel

I want to use void skb_unlink(struct sk_buff *skb, struct sk_buff_head *list) but I do not know how can I get the struct sk_buff_head *list SKB is on?

Related

Re-Order struct to save memory?

I was reading about struct ordering to save memory. I got below TatProduct struct which I am trying to reorder so that I can save some memory but I am not able to figure out on how to order it so that I can save memory?
type CustomerData struct {
Name map[Customer]map[int]string
}
type Customer string
type TatProduct struct {
Ptr string
ProductId int64
Catalogs []int32
Categories []int
BaseProductId *int64
Code int
IsCurrent bool
CommonValue
}
type YeatStringValue struct {
Value string
ClientId int64
}
type CommonValue struct {
Stuff []YeatStringValue
Carts []YeatStringValue
CustomerData
}
Is there any utility that can help me analyze my struct?

golang struct with C struct in CGO

I will use cgo to wrap one c library as go library for the project usage. I read the document, it seems there are lots of rules while using cgo. I don't know whether this is legal or not.
Both LibCtx and Client is a struct in C. Is this a legal way to put C struct into a golang struct?
//DBClientLib.go
type DBClient struct {
Libctx C.LibCtx
LibClient C.Client
}
func (client DBClient) GetEntry(key string) interface{} {
//...
}
Yes, this is totally legal. Check out this short example:
package main
/*
typedef struct Point {
int x , y;
} Point;
*/
import "C"
import "fmt"
type CPoint struct {
Point C.Point
}
func main() {
point := CPoint{Point: C.Point{x: 1, y: 2}}
fmt.Printf("%+v", point)
}
OUTPUT
{Point:{x:1 y:2}}

type INode* is pointer to interface, not interface

So trying out go for the first time today and keep running into an error to do with interfaces, I guess I don't understand them correctly. Ive tried looking around for an answer but the terminology that I'm used to is a little different from other languages so I can't piece it together. As practice I decided to implement a very simple linked list but the error I recieve is:
type INode* is pointer to interface, not interface when calling .setNext(node *Inode)
What is the reason behind this? what piece of information am I missing with interfaces?
Heres the incomplete implementation:
package main
type object interface{}
type INode interface {
GetData() object
GetNext() *INode
setNext(node *INode)
}
type ILinkedList interface {
Link(node *INode)
Unlink(node *INode)
CurrentLength() int
RemoveAt(idx int)
}
type Node struct {
data object
next *INode
}
func (n *Node) GetData() object {
return n.data
}
func (n *Node) GetNext() *INode {
return n.next
}
func (n *Node) setNext(node *INode) {
n.next = node
}
type LinkedList struct {
cur *INode
last *INode
length int
}
func (l *LinkedList) Link(node *INode) {
if l == nil {
return
}
if l.cur == nil {
l.cur = node
l.last = node
} else {
l.last.setNext(node)
l.last = node
}
l.length = l.length + 1
}
This is because in Go, an interface is just a specification of behavior. This behavior can be implemented with either a pointer receiver or a value receiver. The interface doesn't care which one is ultimately used, just as long as it fulfills the interface contract.
See this example:
https://play.golang.org/p/0AaBhB1MHBc
type I interface {
M()
}
type T struct {
S string
}
func (t T) M(){
fmt.Println("T.M fired");
}
type S struct {
S string
}
func (s *S) M(){
fmt.Println("*S.M fired");
}
func RunM(i I){
i.M()
}
func main() {
test1 := T{}
test2 := &S{}
RunM(test1)
RunM(test2)
fmt.Println("Hello, playground")
}
Both pointers to the S type and T types implement the interface I, and can be passed in to any func requiring an I. The interface doesn't care if it's a pointer or not.
You can read up about pointer receivers here: https://tour.golang.org/methods/4
I thought I would post a reference for those visiting in the future that have the same issue regarding pointers to interfaces:
When should I use a pointer to an interface?
Almost never. Pointers to interface values arise only in rare, tricky
situations involving disguising an interface value's type for delayed
evaluation.
It is however a common mistake to pass a pointer to an interface value
to a function expecting an interface. The compiler will complain about
this error but the situation can still be confusing, because sometimes
a pointer is necessary to satisfy an interface. The insight is that
although a pointer to a concrete type can satisfy an interface, with
one exception a pointer to an interface can never satisfy an
interface.
Consider the variable declaration,
var w io.Writer The printing function fmt.Fprintf takes as its first
argument a value that satisfies io.Writer—something that implements
the canonical Write method. Thus we can write
fmt.Fprintf(w, "hello, world\n") If however we pass the address of w,
the program will not compile.
fmt.Fprintf(&w, "hello, world\n") // Compile-time error. The one
exception is that any value, even a pointer to an interface, can be
assigned to a variable of empty interface type (interface{}). Even so,
it's almost certainly a mistake if the value is a pointer to an
interface; the result can be confusing.

using a map which store struct field name and type to recover Go struct

Playground for my problem: https://play.golang.org/p/rVwEaxpkJGL
I have struct like
type SimpleBbInput struct {
MyInput struct {
Num struct {
val int
}
}
HisInput string
HerInput uint8
}
I will store the field name and type in a map[string]interface{}, result like
map[HerInput:uint8 MyInput:map[Num:map[val:int]] HisInput:string]
My question is how to using this map to get back SimpleBbInput without knowing this struct.
Thanks

Golang struct definition patterns

I'm currently playing with Go, was wondering what are the patterns for defining the data types. For example take the Bencode and representing it as a Go data structure.
like in Haskell
data BEncode = BInt Integer
| BString L.ByteString
| BList [BEncode]
| BDict (Map String BEncode)
in C, we can do something like this
struct Bencoding;
typedef struct ListNode {
struct Bencoding *cargo;
struct ListNode *next;
} ListNode;
typedef struct DictNode {
char *key;
struct Bencoding *value;
struct DictNode *next;
} DictNode;
typedef struct Bencoding {
BType type;
union {
long long val; // used when type == BInt
ListNode *list; // used when type == BList
char *str; // used when type == BString
DictNode *dict;
} cargo; // data
} Bencoding;
what is the best way to define these kinds of data structures in Golang. Are there any patterns / good practices with Golang.
Like this?
type BEncodeType int
const (
TypeBInt BEncodeType = iota
TypeBString
TypeBList
TypeBDict
)
type BEncode interface {
Type() BEncodeType
Val() interface{}
}
type BInt int
func (n *BInt) Type() BEncodeType {
return TypeBInt
}
func (n *BInt) Val() interface{} {
return n
}
type BString string
func (n *BString) Type() BEncodeType {
return TypeBString
}
func (n *BString) Val() interface{} {
return n
}
type BList []BEncode
func (n *BList) Type() BEncodeType {
return TypeBList
}
func (n *BList) Val() interface{} {
return n
}
type BDict map[string]BEncode
func (n *BDict) Type() BEncodeType {
return TypeBDict
}
func (n *BDict) Val() interface{} {
return n
}

Resources