for loop on kernel space

Hi Kiran,

in python program if i have a list or tuple and etc i can for loop very easy,also in C program if i have array i can access to value with index,

python example:

list = [1,2,3,4]
for i in list:
print i

output :
1
2
3
4

or C :

int main(){
int arr[] = {1,2,3,4}
int i = 0;
for (i; i <= sizeof(arr);i++){
printf("%d\n", i);
}
}

output :

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


now i have question in for_each_net(...) http://elixir.free-electrons.com/linux/latest/source/net/core/dev.c#L1550,

how to work this for loop? you can example how to work struct net in loop "for_each_net(net)"?





Best regrads

Comments

  • Hi Ramin,

    for_each_net(...) is not a standard API in C. It is implemented somewhere as you can see here:

    http://elixir.free-electrons.com/linux/latest/source/include/net/net_namespace.h#L266

    points to:
    #define for_each_net(VAR) \
    list_for_each_entry(VAR, &net_namespace_list, list)

    #define for_each_net_rcu(VAR) \
    list_for_each_entry_rcu(VAR, &net_namespace_list, list)

    and this points to:
    http://elixir.free-electrons.com/linux/latest/ident/list_for_each_entry

    http://elixir.free-electrons.com/linux/latest/source/include/linux/list.h#L462
    and to this:
    /**
    * list_for_each_entry - iterate over list of given type
    * @pos: the type * to use as a loop cursor.
    * @head: the head for your list.
    * @member: the name of the list_head within the struct.
    */
    #define list_for_each_entry(pos, head, member) \
    for (pos = list_first_entry(head, typeof(*pos), member); \
    &pos->member != (head); \
    pos = list_next_entry(pos, member))

    So if you do user-space C programming try avoiding APIs like: sizeof(arr)

    Instead have a manual counter corresponds to the array. This is not needed in scripting languages php/python, etc.
    But in C, it is lot more things are manual and need to be safe.

    For example if this array is type of some complex struct then ?

    struct abc list[10];

    And if abc contains:

    {
    int a;
    char *buff;
    }

    This makes it complicated.

    So have a counter.

    int arr[] = {1,2,3,4};
    int arr_size = 4; // -> hardcode or mention dynamically this way.

    cheers, Kiran
  • or the other option to use dynamically a static array is this way as I discussed in the look-up table video:



  • edited June 29
    also sizeof(arr) gets you wrong output, and hence the loop executed for 0-16 (which is around 17 iterations.

    sizeof() gets the size of a data-type in general. But array is a collection of items. And so the confusion. So in general always this is the reason an independent length field (or variable) is maintained.

    for example we have:
    skb->data (is a char buffer holding the pkt/frame bytes)
    and then we have skb->len (which tells the length of the skb->data bytes).

    so if you want to print the contents of access, then its:
    memcpy(abc, skb->data, skb->len);

    or

    for(i=0ilen;i++)
    {
    printk("%02x ", skb->data);
    }
    printk("\n");

    we cannot do: sizeof(skb->data) !!

    NOTE: let us assume in this case this skbuff is not fragmented. So doing skb->len is bit valid ;)

    cheers, Kiran
Sign In or Register to comment.