tipc: let broadcast transmission use new link transmit function
[cascardo/linux.git] / net / tipc / link.c
index e7c6086..363da5f 100644 (file)
@@ -50,6 +50,7 @@
  */
 static const char *link_co_err = "Link tunneling error, ";
 static const char *link_rst_msg = "Resetting link ";
+static const char tipc_bclink_name[] = "broadcast-link";
 
 static const struct nla_policy tipc_nl_link_policy[TIPC_NLA_LINK_MAX + 1] = {
        [TIPC_NLA_LINK_UNSPEC]          = { .type = NLA_UNSPEC },
@@ -120,11 +121,21 @@ bool tipc_link_is_up(struct tipc_link *l)
        return link_is_up(l);
 }
 
+bool tipc_link_peer_is_down(struct tipc_link *l)
+{
+       return l->state == LINK_PEER_RESET;
+}
+
 bool tipc_link_is_reset(struct tipc_link *l)
 {
        return l->state & (LINK_RESET | LINK_FAILINGOVER | LINK_ESTABLISHING);
 }
 
+bool tipc_link_is_establishing(struct tipc_link *l)
+{
+       return l->state == LINK_ESTABLISHING;
+}
+
 bool tipc_link_is_synching(struct tipc_link *l)
 {
        return l->state == LINK_SYNCHING;
@@ -147,6 +158,21 @@ int tipc_link_is_active(struct tipc_link *l)
        return (node_active_link(n, 0) == l) || (node_active_link(n, 1) == l);
 }
 
+void tipc_link_add_bc_peer(struct tipc_link *l)
+{
+       l->ackers++;
+}
+
+void tipc_link_remove_bc_peer(struct tipc_link *l)
+{
+       l->ackers--;
+}
+
+int tipc_link_bc_peers(struct tipc_link *l)
+{
+       return l->ackers;
+}
+
 static u32 link_own_addr(struct tipc_link *l)
 {
        return msg_prevnode(l->pmsg);
@@ -155,9 +181,16 @@ static u32 link_own_addr(struct tipc_link *l)
 /**
  * tipc_link_create - create a new link
  * @n: pointer to associated node
- * @b: pointer to associated bearer
+ * @if_name: associated interface name
+ * @bearer_id: id (index) of associated bearer
+ * @tolerance: link tolerance to be used by link
+ * @net_plane: network plane (A,B,c..) this link belongs to
+ * @mtu: mtu to be advertised by link
+ * @priority: priority to be used by link
+ * @window: send window to be used by link
+ * @session: session to be used by link
  * @ownnode: identity of own node
- * @peer: identity of peer node
+ * @peer: node id of peer node
  * @maddr: media address to be used
  * @inputq: queue to put messages ready for delivery
  * @namedq: queue to put binding table update messages ready for delivery
@@ -165,47 +198,48 @@ static u32 link_own_addr(struct tipc_link *l)
  *
  * Returns true if link was created, otherwise false
  */
-bool tipc_link_create(struct tipc_node *n, struct tipc_bearer *b, u32 session,
-                     u32 ownnode, u32 peer, struct tipc_media_addr *maddr,
+bool tipc_link_create(struct tipc_node *n, char *if_name, int bearer_id,
+                     int tolerance, char net_plane, u32 mtu, int priority,
+                     int window, u32 session, u32 ownnode, u32 peer,
+                     struct tipc_media_addr *maddr,
                      struct sk_buff_head *inputq, struct sk_buff_head *namedq,
                      struct tipc_link **link)
 {
        struct tipc_link *l;
        struct tipc_msg *hdr;
-       char *if_name;
 
        l = kzalloc(sizeof(*l), GFP_ATOMIC);
        if (!l)
                return false;
        *link = l;
+       l->pmsg = (struct tipc_msg *)&l->proto_msg;
+       hdr = l->pmsg;
+       tipc_msg_init(ownnode, hdr, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, peer);
+       msg_set_size(hdr, sizeof(l->proto_msg));
+       msg_set_session(hdr, session);
+       msg_set_bearer_id(hdr, l->bearer_id);
 
        /* Note: peer i/f name is completed by reset/activate message */
-       if_name = strchr(b->name, ':') + 1;
        sprintf(l->name, "%u.%u.%u:%s-%u.%u.%u:unknown",
                tipc_zone(ownnode), tipc_cluster(ownnode), tipc_node(ownnode),
                if_name, tipc_zone(peer), tipc_cluster(peer), tipc_node(peer));
+       strcpy((char *)msg_data(hdr), if_name);
 
        l->addr = peer;
        l->media_addr = maddr;
        l->owner = n;
        l->peer_session = WILDCARD_SESSION;
-       l->bearer_id = b->identity;
-       l->tolerance = b->tolerance;
-       l->net_plane = b->net_plane;
-       l->advertised_mtu = b->mtu;
-       l->mtu = b->mtu;
-       l->priority = b->priority;
-       tipc_link_set_queue_limits(l, b->window);
+       l->bearer_id = bearer_id;
+       l->tolerance = tolerance;
+       l->net_plane = net_plane;
+       l->advertised_mtu = mtu;
+       l->mtu = mtu;
+       l->priority = priority;
+       tipc_link_set_queue_limits(l, window);
+       l->ackers = 1;
        l->inputq = inputq;
        l->namedq = namedq;
        l->state = LINK_RESETTING;
-       l->pmsg = (struct tipc_msg *)&l->proto_msg;
-       hdr = l->pmsg;
-       tipc_msg_init(ownnode, hdr, LINK_PROTOCOL, RESET_MSG, INT_H_SIZE, peer);
-       msg_set_size(hdr, sizeof(l->proto_msg));
-       msg_set_session(hdr, session);
-       msg_set_bearer_id(hdr, l->bearer_id);
-       strcpy((char *)msg_data(hdr), if_name);
        __skb_queue_head_init(&l->transmq);
        __skb_queue_head_init(&l->backlogq);
        __skb_queue_head_init(&l->deferdq);
@@ -214,6 +248,35 @@ bool tipc_link_create(struct tipc_node *n, struct tipc_bearer *b, u32 session,
        return true;
 }
 
+/**
+ * tipc_link_bc_create - create new link to be used for broadcast
+ * @n: pointer to associated node
+ * @mtu: mtu to be used
+ * @window: send window to be used
+ * @inputq: queue to put messages ready for delivery
+ * @namedq: queue to put binding table update messages ready for delivery
+ * @link: return value, pointer to put the created link
+ *
+ * Returns true if link was created, otherwise false
+ */
+bool tipc_link_bc_create(struct tipc_node *n, int mtu, int window,
+                        struct sk_buff_head *inputq,
+                        struct sk_buff_head *namedq,
+                        struct tipc_link **link)
+{
+       struct tipc_link *l;
+
+       if (!tipc_link_create(n, "", MAX_BEARERS, 0, 'Z', mtu, 0, window,
+                             0, 0, 0, NULL, inputq, namedq, link))
+               return false;
+
+       l = *link;
+       strcpy(l->name, tipc_bclink_name);
+       tipc_link_reset(l);
+       l->ackers = 0;
+       return true;
+}
+
 /* tipc_link_build_bcast_sync_msg() - synchronize broadcast link endpoints.
  *
  * Give a newly added peer node the sequence number where it should
@@ -321,14 +384,15 @@ int tipc_link_fsm_evt(struct tipc_link *l, int evt)
                switch (evt) {
                case LINK_ESTABLISH_EVT:
                        l->state = LINK_ESTABLISHED;
-                       rc |= TIPC_LINK_UP_EVT;
                        break;
                case LINK_FAILOVER_BEGIN_EVT:
                        l->state = LINK_FAILINGOVER;
                        break;
-               case LINK_PEER_RESET_EVT:
                case LINK_RESET_EVT:
+                       l->state = LINK_RESET;
+                       break;
                case LINK_FAILURE_EVT:
+               case LINK_PEER_RESET_EVT:
                case LINK_SYNCH_BEGIN_EVT:
                case LINK_FAILOVER_END_EVT:
                        break;
@@ -578,8 +642,6 @@ void tipc_link_purge_queues(struct tipc_link *l_ptr)
 
 void tipc_link_reset(struct tipc_link *l)
 {
-       tipc_link_fsm_evt(l, LINK_RESET_EVT);
-
        /* Link is down, accept any session */
        l->peer_session = WILDCARD_SESSION;
 
@@ -602,6 +664,7 @@ void tipc_link_reset(struct tipc_link *l)
        l->rcv_unacked = 0;
        l->snd_nxt = 1;
        l->rcv_nxt = 1;
+       l->acked = 0;
        l->silent_intv_cnt = 0;
        l->stats.recv_info = 0;
        l->stale_count = 0;
@@ -724,6 +787,7 @@ int tipc_link_xmit(struct tipc_link *l, struct sk_buff_head *list,
                        __skb_dequeue(list);
                        __skb_queue_tail(transmq, skb);
                        __skb_queue_tail(xmitq, _skb);
+                       TIPC_SKB_CB(skb)->ackers = l->ackers;
                        l->rcv_unacked = 0;
                        seqno++;
                        continue;
@@ -784,6 +848,7 @@ void tipc_link_push_packets(struct tipc_link *link)
                skb = __skb_dequeue(&link->backlogq);
                if (!skb)
                        break;
+               TIPC_SKB_CB(skb)->ackers = link->ackers;
                msg = buf_msg(skb);
                link->backlog[msg_importance(msg)].len--;
                msg_set_ack(msg, ack);
@@ -817,6 +882,7 @@ void tipc_link_advance_backlog(struct tipc_link *l, struct sk_buff_head *xmitq)
                l->backlog[msg_importance(hdr)].len--;
                __skb_queue_tail(&l->transmq, skb);
                __skb_queue_tail(xmitq, _skb);
+               TIPC_SKB_CB(skb)->ackers = l->ackers;
                msg_set_ack(hdr, ack);
                msg_set_seqno(hdr, seqno);
                msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
@@ -848,8 +914,7 @@ static void link_retransmit_failure(struct tipc_link *l_ptr,
                char addr_string[16];
 
                pr_info("Msg seq number: %u,  ", msg_seqno(msg));
-               pr_cont("Outstanding acks: %lu\n",
-                       (unsigned long) TIPC_SKB_CB(buf)->handle);
+               pr_cont("Outstanding acks: %u\n", TIPC_SKB_CB(buf)->ackers);
 
                n_ptr = tipc_bclink_retransmit_to(net);
 
@@ -902,11 +967,13 @@ void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
        }
 }
 
-static int tipc_link_retransm(struct tipc_link *l, int retransm,
-                             struct sk_buff_head *xmitq)
+int tipc_link_retrans(struct tipc_link *l, u16 from, u16 to,
+                     struct sk_buff_head *xmitq)
 {
        struct sk_buff *_skb, *skb = skb_peek(&l->transmq);
        struct tipc_msg *hdr;
+       u16 ack = l->rcv_nxt - 1;
+       u16 bc_ack = l->owner->bclink.last_in;
 
        if (!skb)
                return 0;
@@ -919,19 +986,25 @@ static int tipc_link_retransm(struct tipc_link *l, int retransm,
                link_retransmit_failure(l, skb);
                return tipc_link_fsm_evt(l, LINK_FAILURE_EVT);
        }
+
+       /* Move forward to where retransmission should start */
        skb_queue_walk(&l->transmq, skb) {
-               if (!retransm)
-                       return 0;
+               if (!less(buf_seqno(skb), from))
+                       break;
+       }
+
+       skb_queue_walk_from(&l->transmq, skb) {
+               if (more(buf_seqno(skb), to))
+                       break;
                hdr = buf_msg(skb);
                _skb = __pskb_copy(skb, MIN_H_SIZE, GFP_ATOMIC);
                if (!_skb)
                        return 0;
                hdr = buf_msg(_skb);
-               msg_set_ack(hdr, l->rcv_nxt - 1);
-               msg_set_bcast_ack(hdr, l->owner->bclink.last_in);
+               msg_set_ack(hdr, ack);
+               msg_set_bcast_ack(hdr, bc_ack);
                _skb->priority = TC_PRIO_CONTROL;
                __skb_queue_tail(xmitq, _skb);
-               retransm--;
                l->stats.retransmitted++;
        }
        return 0;
@@ -1056,6 +1129,18 @@ void tipc_link_build_ack_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
        tipc_link_build_proto_msg(l, STATE_MSG, 0, 0, 0, 0, xmitq);
 }
 
+/* tipc_link_build_reset_msg: prepare link RESET or ACTIVATE message
+ */
+void tipc_link_build_reset_msg(struct tipc_link *l, struct sk_buff_head *xmitq)
+{
+       int mtyp = RESET_MSG;
+
+       if (l->state == LINK_ESTABLISHING)
+               mtyp = ACTIVATE_MSG;
+
+       tipc_link_build_proto_msg(l, mtyp, 0, 0, 0, 0, xmitq);
+}
+
 /* tipc_link_build_nack_msg: prepare link nack message for transmission
  */
 static void tipc_link_build_nack_msg(struct tipc_link *l,
@@ -1091,9 +1176,9 @@ int tipc_link_rcv(struct tipc_link *l, struct sk_buff *skb,
                        return tipc_link_proto_rcv(l, skb, xmitq);
 
                if (unlikely(!link_is_up(l))) {
-                       rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
-                       if (!link_is_up(l))
-                               goto drop;
+                       if (l->state == LINK_ESTABLISHING)
+                               rc = TIPC_LINK_UP_EVT;
+                       goto drop;
                }
 
                /* Don't send probe at next timeout expiration */
@@ -1333,11 +1418,13 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
 {
        struct tipc_msg *hdr = buf_msg(skb);
        u16 rcvgap = 0;
-       u16 nacked_gap = msg_seq_gap(hdr);
+       u16 ack = msg_ack(hdr);
+       u16 gap = msg_seq_gap(hdr);
        u16 peers_snd_nxt =  msg_next_sent(hdr);
        u16 peers_tol = msg_link_tolerance(hdr);
        u16 peers_prio = msg_linkprio(hdr);
        u16 rcv_nxt = l->rcv_nxt;
+       int mtyp = msg_type(hdr);
        char *if_name;
        int rc = 0;
 
@@ -1347,7 +1434,7 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
        if (link_own_addr(l) > msg_prevnode(hdr))
                l->net_plane = msg_net_plane(hdr);
 
-       switch (msg_type(hdr)) {
+       switch (mtyp) {
        case RESET_MSG:
 
                /* Ignore duplicate RESET with old session number */
@@ -1374,12 +1461,14 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
                if (in_range(peers_prio, l->priority + 1, TIPC_MAX_LINK_PRI))
                        l->priority = peers_prio;
 
-               if (msg_type(hdr) == RESET_MSG) {
-                       rc |= tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
-               } else if (!link_is_up(l)) {
-                       tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
-                       rc |= tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
-               }
+               /* ACTIVATE_MSG serves as PEER_RESET if link is already down */
+               if ((mtyp == RESET_MSG) || !link_is_up(l))
+                       rc = tipc_link_fsm_evt(l, LINK_PEER_RESET_EVT);
+
+               /* ACTIVATE_MSG takes up link if it was already locally reset */
+               if ((mtyp == ACTIVATE_MSG) && (l->state == LINK_ESTABLISHING))
+                       rc = TIPC_LINK_UP_EVT;
+
                l->peer_session = msg_session(hdr);
                l->peer_bearer_id = msg_bearer_id(hdr);
                if (l->mtu > msg_max_pkt(hdr))
@@ -1396,9 +1485,12 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
                l->stats.recv_states++;
                if (msg_probe(hdr))
                        l->stats.recv_probes++;
-               rc = tipc_link_fsm_evt(l, LINK_ESTABLISH_EVT);
-               if (!link_is_up(l))
+
+               if (!link_is_up(l)) {
+                       if (l->state == LINK_ESTABLISHING)
+                               rc = TIPC_LINK_UP_EVT;
                        break;
+               }
 
                /* Send NACK if peer has sent pkts we haven't received yet */
                if (more(peers_snd_nxt, rcv_nxt) && !tipc_link_is_synching(l))
@@ -1406,11 +1498,11 @@ static int tipc_link_proto_rcv(struct tipc_link *l, struct sk_buff *skb,
                if (rcvgap || (msg_probe(hdr)))
                        tipc_link_build_proto_msg(l, STATE_MSG, 0, rcvgap,
                                                  0, 0, xmitq);
-               tipc_link_release_pkts(l, msg_ack(hdr));
+               tipc_link_release_pkts(l, ack);
 
                /* If NACK, retransmit will now start at right position */
-               if (nacked_gap) {
-                       rc = tipc_link_retransm(l, nacked_gap, xmitq);
+               if (gap) {
+                       rc = tipc_link_retrans(l, ack + 1, ack + gap, xmitq);
                        l->stats.recv_nacks++;
                }
 
@@ -1487,7 +1579,7 @@ static void link_reset_statistics(struct tipc_link *l_ptr)
 static void link_print(struct tipc_link *l, const char *str)
 {
        struct sk_buff *hskb = skb_peek(&l->transmq);
-       u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt;
+       u16 head = hskb ? msg_seqno(buf_msg(hskb)) : l->snd_nxt - 1;
        u16 tail = l->snd_nxt - 1;
 
        pr_info("%s Link <%s> state %x\n", str, l->name, l->state);