ip_append_data中的一个问题

时间:2014-10-09 15:19:25

标签: linux networking kernel

当我看到这些代码时,我对linux网络协议栈中的ip_append_data感到困惑:

    if (!(rt->u.dst.dev->features&NETIF_F_SG)) {
        unsigned int off;

        off = skb->len;
        if (getfrag(from, skb_put(skb, copy), 
                offset, copy, off, skb) < 0) {
            __skb_trim(skb, off);
            err = -EFAULT;
            goto error;
        }
    } else {
        int i = skb_shinfo(skb)->nr_frags;
        skb_frag_t *frag = &skb_shinfo(skb)->frags[i-1];
        struct page *page = sk->sk_sndmsg_page;
        int off = sk->sk_sndmsg_off;
        unsigned int left;

        if (page && (left = PAGE_SIZE - off) > 0) {
            if (copy >= left)
                copy = left;
            if (page != frag->page) {
                if (i == MAX_SKB_FRAGS) {
                    err = -EMSGSIZE;
                    goto error;
                }
                get_page(page);
                skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
                frag = &skb_shinfo(skb)->frags[i];
            }
        } else if (i < MAX_SKB_FRAGS) {
            if (copy > PAGE_SIZE)
                copy = PAGE_SIZE;
            page = alloc_pages(sk->sk_allocation, 0);
            if (page == NULL)  {
                err = -ENOMEM;
                goto error;
            }
            sk->sk_sndmsg_page = page;
            sk->sk_sndmsg_off = 0;

            skb_fill_page_desc(skb, i, page, 0, 0);
            frag = &skb_shinfo(skb)->frags[i];
            skb->truesize += PAGE_SIZE;
            atomic_add(PAGE_SIZE, &sk->sk_wmem_alloc);
        } else {
            err = -EMSGSIZE;
            goto error;
        }
        if (getfrag(from, page_address(frag->page)+frag->page_offset+frag->size, offset, copy, skb->len, skb) < 0) {
            err = -EFAULT;
            goto error;
        }
        sk->sk_sndmsg_off += copy;
        frag->size += copy;
        skb->len += copy;
        skb->data_len += copy;
    }

我的问题如下:

条件是(页面!= frag-&gt;页面),为什么?可以用(!frag-&gt;页面)代替吗?当(!frag-&gt; page)为真时,我认为(page == frag-&gt; page)始终为true。希望有人告诉我原因。

            if **(page != frag->page)** {
                if (i == MAX_SKB_FRAGS) {
                    err = -EMSGSIZE;
                    goto error;
                }
                get_page(page);
                skb_fill_page_desc(skb, i, page, sk->sk_sndmsg_off, 0);
                frag = &skb_shinfo(skb)->frags[i];
            }

1 个答案:

答案 0 :(得分:0)

  

条件是(page != frag->page),为什么?它可以被替换   (!frag->page)?我认为(page == frag->page)总是如此   (!frag->page)是真的。

如果(page == frag->page)为真, (!frag->page)始终为真(page != frag->page)为真时(!frag->page)为假,并替换为{{1} } (page != frag->page)将把false替换为true。

如果您打算写{em> (!frag->page)(page != frag->page)为真时始终为真,如果这是正确的,那对(!frag->page)来说还不够等同于(page != frag->page),因为(!frag->page)(page != frag->page)为假时(!frag->page)并不总是错误。