datapath.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  1. /*
  2. * Copyright (c) 2007-2013 Nicira, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/ethtool.h>
  38. #include <linux/wait.h>
  39. #include <asm/div64.h>
  40. #include <linux/highmem.h>
  41. #include <linux/netfilter_bridge.h>
  42. #include <linux/netfilter_ipv4.h>
  43. #include <linux/inetdevice.h>
  44. #include <linux/list.h>
  45. #include <linux/lockdep.h>
  46. #include <linux/openvswitch.h>
  47. #include <linux/rculist.h>
  48. #include <linux/dmi.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/net_namespace.h>
  52. #include <net/netns/generic.h>
  53. #include "datapath.h"
  54. #include "flow.h"
  55. #include "flow_table.h"
  56. #include "flow_netlink.h"
  57. #include "vport-internal_dev.h"
  58. #include "vport-netdev.h"
  59. int ovs_net_id __read_mostly;
  60. static void ovs_notify(struct genl_family *family,
  61. struct sk_buff *skb, struct genl_info *info)
  62. {
  63. genl_notify(family, skb, genl_info_net(info), info->snd_portid,
  64. 0, info->nlhdr, GFP_KERNEL);
  65. }
  66. /**
  67. * DOC: Locking:
  68. *
  69. * All writes e.g. Writes to device state (add/remove datapath, port, set
  70. * operations on vports, etc.), Writes to other state (flow table
  71. * modifications, set miscellaneous datapath parameters, etc.) are protected
  72. * by ovs_lock.
  73. *
  74. * Reads are protected by RCU.
  75. *
  76. * There are a few special cases (mostly stats) that have their own
  77. * synchronization but they nest under all of above and don't interact with
  78. * each other.
  79. *
  80. * The RTNL lock nests inside ovs_mutex.
  81. */
  82. static DEFINE_MUTEX(ovs_mutex);
  83. void ovs_lock(void)
  84. {
  85. mutex_lock(&ovs_mutex);
  86. }
  87. void ovs_unlock(void)
  88. {
  89. mutex_unlock(&ovs_mutex);
  90. }
  91. #ifdef CONFIG_LOCKDEP
  92. int lockdep_ovsl_is_held(void)
  93. {
  94. if (debug_locks)
  95. return lockdep_is_held(&ovs_mutex);
  96. else
  97. return 1;
  98. }
  99. #endif
  100. static struct vport *new_vport(const struct vport_parms *);
  101. static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
  102. const struct dp_upcall_info *);
  103. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
  104. const struct dp_upcall_info *);
  105. /* Must be called with rcu_read_lock or ovs_mutex. */
  106. static struct datapath *get_dp(struct net *net, int dp_ifindex)
  107. {
  108. struct datapath *dp = NULL;
  109. struct net_device *dev;
  110. rcu_read_lock();
  111. dev = dev_get_by_index_rcu(net, dp_ifindex);
  112. if (dev) {
  113. struct vport *vport = ovs_internal_dev_get_vport(dev);
  114. if (vport)
  115. dp = vport->dp;
  116. }
  117. rcu_read_unlock();
  118. return dp;
  119. }
  120. /* Must be called with rcu_read_lock or ovs_mutex. */
  121. static const char *ovs_dp_name(const struct datapath *dp)
  122. {
  123. struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
  124. return vport->ops->get_name(vport);
  125. }
  126. static int get_dpifindex(struct datapath *dp)
  127. {
  128. struct vport *local;
  129. int ifindex;
  130. rcu_read_lock();
  131. local = ovs_vport_rcu(dp, OVSP_LOCAL);
  132. if (local)
  133. ifindex = netdev_vport_priv(local)->dev->ifindex;
  134. else
  135. ifindex = 0;
  136. rcu_read_unlock();
  137. return ifindex;
  138. }
  139. static void destroy_dp_rcu(struct rcu_head *rcu)
  140. {
  141. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  142. free_percpu(dp->stats_percpu);
  143. release_net(ovs_dp_get_net(dp));
  144. kfree(dp->ports);
  145. kfree(dp);
  146. }
  147. static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
  148. u16 port_no)
  149. {
  150. return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
  151. }
  152. struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
  153. {
  154. struct vport *vport;
  155. struct hlist_head *head;
  156. head = vport_hash_bucket(dp, port_no);
  157. hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
  158. if (vport->port_no == port_no)
  159. return vport;
  160. }
  161. return NULL;
  162. }
  163. /* Called with ovs_mutex. */
  164. static struct vport *new_vport(const struct vport_parms *parms)
  165. {
  166. struct vport *vport;
  167. vport = ovs_vport_add(parms);
  168. if (!IS_ERR(vport)) {
  169. struct datapath *dp = parms->dp;
  170. struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
  171. hlist_add_head_rcu(&vport->dp_hash_node, head);
  172. }
  173. return vport;
  174. }
  175. void ovs_dp_detach_port(struct vport *p)
  176. {
  177. ASSERT_OVSL();
  178. /* First drop references to device. */
  179. hlist_del_rcu(&p->dp_hash_node);
  180. /* Then destroy it. */
  181. ovs_vport_del(p);
  182. }
  183. /* Must be called with rcu_read_lock. */
  184. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  185. {
  186. struct datapath *dp = p->dp;
  187. struct sw_flow *flow;
  188. struct dp_stats_percpu *stats;
  189. struct sw_flow_key key;
  190. u64 *stats_counter;
  191. u32 n_mask_hit;
  192. int error;
  193. stats = this_cpu_ptr(dp->stats_percpu);
  194. /* Extract flow from 'skb' into 'key'. */
  195. error = ovs_flow_extract(skb, p->port_no, &key);
  196. if (unlikely(error)) {
  197. kfree_skb(skb);
  198. return;
  199. }
  200. /* Look up flow. */
  201. flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
  202. if (unlikely(!flow)) {
  203. struct dp_upcall_info upcall;
  204. upcall.cmd = OVS_PACKET_CMD_MISS;
  205. upcall.key = &key;
  206. upcall.userdata = NULL;
  207. upcall.portid = p->upcall_portid;
  208. ovs_dp_upcall(dp, skb, &upcall);
  209. consume_skb(skb);
  210. stats_counter = &stats->n_missed;
  211. goto out;
  212. }
  213. OVS_CB(skb)->flow = flow;
  214. OVS_CB(skb)->pkt_key = &key;
  215. ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
  216. ovs_execute_actions(dp, skb);
  217. stats_counter = &stats->n_hit;
  218. out:
  219. /* Update datapath statistics. */
  220. u64_stats_update_begin(&stats->sync);
  221. (*stats_counter)++;
  222. stats->n_mask_hit += n_mask_hit;
  223. u64_stats_update_end(&stats->sync);
  224. }
  225. static struct genl_family dp_packet_genl_family = {
  226. .id = GENL_ID_GENERATE,
  227. .hdrsize = sizeof(struct ovs_header),
  228. .name = OVS_PACKET_FAMILY,
  229. .version = OVS_PACKET_VERSION,
  230. .maxattr = OVS_PACKET_ATTR_MAX,
  231. .netnsok = true,
  232. .parallel_ops = true,
  233. };
  234. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  235. const struct dp_upcall_info *upcall_info)
  236. {
  237. struct dp_stats_percpu *stats;
  238. int err;
  239. if (upcall_info->portid == 0) {
  240. err = -ENOTCONN;
  241. goto err;
  242. }
  243. if (!skb_is_gso(skb))
  244. err = queue_userspace_packet(dp, skb, upcall_info);
  245. else
  246. err = queue_gso_packets(dp, skb, upcall_info);
  247. if (err)
  248. goto err;
  249. return 0;
  250. err:
  251. stats = this_cpu_ptr(dp->stats_percpu);
  252. u64_stats_update_begin(&stats->sync);
  253. stats->n_lost++;
  254. u64_stats_update_end(&stats->sync);
  255. return err;
  256. }
  257. static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
  258. const struct dp_upcall_info *upcall_info)
  259. {
  260. unsigned short gso_type = skb_shinfo(skb)->gso_type;
  261. struct dp_upcall_info later_info;
  262. struct sw_flow_key later_key;
  263. struct sk_buff *segs, *nskb;
  264. int err;
  265. segs = __skb_gso_segment(skb, NETIF_F_SG, false);
  266. if (IS_ERR(segs))
  267. return PTR_ERR(segs);
  268. /* Queue all of the segments. */
  269. skb = segs;
  270. do {
  271. err = queue_userspace_packet(dp, skb, upcall_info);
  272. if (err)
  273. break;
  274. if (skb == segs && gso_type & SKB_GSO_UDP) {
  275. /* The initial flow key extracted by ovs_flow_extract()
  276. * in this case is for a first fragment, so we need to
  277. * properly mark later fragments.
  278. */
  279. later_key = *upcall_info->key;
  280. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  281. later_info = *upcall_info;
  282. later_info.key = &later_key;
  283. upcall_info = &later_info;
  284. }
  285. } while ((skb = skb->next));
  286. /* Free all of the segments. */
  287. skb = segs;
  288. do {
  289. nskb = skb->next;
  290. if (err)
  291. kfree_skb(skb);
  292. else
  293. consume_skb(skb);
  294. } while ((skb = nskb));
  295. return err;
  296. }
  297. static size_t key_attr_size(void)
  298. {
  299. return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
  300. + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
  301. + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
  302. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
  303. + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
  304. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
  305. + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
  306. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
  307. + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
  308. + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
  309. + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
  310. + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
  311. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  312. + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
  313. + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
  314. + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
  315. + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
  316. + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
  317. + nla_total_size(28); /* OVS_KEY_ATTR_ND */
  318. }
  319. static size_t upcall_msg_size(const struct nlattr *userdata,
  320. unsigned int hdrlen)
  321. {
  322. size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
  323. + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
  324. + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
  325. /* OVS_PACKET_ATTR_USERDATA */
  326. if (userdata)
  327. size += NLA_ALIGN(userdata->nla_len);
  328. return size;
  329. }
  330. static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
  331. const struct dp_upcall_info *upcall_info)
  332. {
  333. struct ovs_header *upcall;
  334. struct sk_buff *nskb = NULL;
  335. struct sk_buff *user_skb; /* to be queued to userspace */
  336. struct nlattr *nla;
  337. struct genl_info info = {
  338. .dst_sk = ovs_dp_get_net(dp)->genl_sock,
  339. .snd_portid = upcall_info->portid,
  340. };
  341. size_t len;
  342. unsigned int hlen;
  343. int err, dp_ifindex;
  344. dp_ifindex = get_dpifindex(dp);
  345. if (!dp_ifindex)
  346. return -ENODEV;
  347. if (vlan_tx_tag_present(skb)) {
  348. nskb = skb_clone(skb, GFP_ATOMIC);
  349. if (!nskb)
  350. return -ENOMEM;
  351. nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
  352. if (!nskb)
  353. return -ENOMEM;
  354. nskb->vlan_tci = 0;
  355. skb = nskb;
  356. }
  357. if (nla_attr_size(skb->len) > USHRT_MAX) {
  358. err = -EFBIG;
  359. goto out;
  360. }
  361. /* Complete checksum if needed */
  362. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  363. (err = skb_checksum_help(skb)))
  364. goto out;
  365. /* Older versions of OVS user space enforce alignment of the last
  366. * Netlink attribute to NLA_ALIGNTO which would require extensive
  367. * padding logic. Only perform zerocopy if padding is not required.
  368. */
  369. if (dp->user_features & OVS_DP_F_UNALIGNED)
  370. hlen = skb_zerocopy_headlen(skb);
  371. else
  372. hlen = skb->len;
  373. len = upcall_msg_size(upcall_info->userdata, hlen);
  374. user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
  375. if (!user_skb) {
  376. err = -ENOMEM;
  377. goto out;
  378. }
  379. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  380. 0, upcall_info->cmd);
  381. upcall->dp_ifindex = dp_ifindex;
  382. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  383. ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
  384. nla_nest_end(user_skb, nla);
  385. if (upcall_info->userdata)
  386. __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
  387. nla_len(upcall_info->userdata),
  388. nla_data(upcall_info->userdata));
  389. /* Only reserve room for attribute header, packet data is added
  390. * in skb_zerocopy() */
  391. if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
  392. err = -ENOBUFS;
  393. goto out;
  394. }
  395. nla->nla_len = nla_attr_size(skb->len);
  396. skb_zerocopy(user_skb, skb, skb->len, hlen);
  397. /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
  398. if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
  399. size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
  400. if (plen > 0)
  401. memset(skb_put(user_skb, plen), 0, plen);
  402. }
  403. ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
  404. err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
  405. out:
  406. kfree_skb(nskb);
  407. return err;
  408. }
  409. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  410. {
  411. struct ovs_header *ovs_header = info->userhdr;
  412. struct nlattr **a = info->attrs;
  413. struct sw_flow_actions *acts;
  414. struct sk_buff *packet;
  415. struct sw_flow *flow;
  416. struct datapath *dp;
  417. struct ethhdr *eth;
  418. int len;
  419. int err;
  420. err = -EINVAL;
  421. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  422. !a[OVS_PACKET_ATTR_ACTIONS])
  423. goto err;
  424. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  425. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  426. err = -ENOMEM;
  427. if (!packet)
  428. goto err;
  429. skb_reserve(packet, NET_IP_ALIGN);
  430. nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
  431. skb_reset_mac_header(packet);
  432. eth = eth_hdr(packet);
  433. /* Normally, setting the skb 'protocol' field would be handled by a
  434. * call to eth_type_trans(), but it assumes there's a sending
  435. * device, which we may not have. */
  436. if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
  437. packet->protocol = eth->h_proto;
  438. else
  439. packet->protocol = htons(ETH_P_802_2);
  440. /* Build an sw_flow for sending this packet. */
  441. flow = ovs_flow_alloc(false);
  442. err = PTR_ERR(flow);
  443. if (IS_ERR(flow))
  444. goto err_kfree_skb;
  445. err = ovs_flow_extract(packet, -1, &flow->key);
  446. if (err)
  447. goto err_flow_free;
  448. err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
  449. if (err)
  450. goto err_flow_free;
  451. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
  452. err = PTR_ERR(acts);
  453. if (IS_ERR(acts))
  454. goto err_flow_free;
  455. err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
  456. &flow->key, 0, &acts);
  457. rcu_assign_pointer(flow->sf_acts, acts);
  458. if (err)
  459. goto err_flow_free;
  460. OVS_CB(packet)->flow = flow;
  461. OVS_CB(packet)->pkt_key = &flow->key;
  462. packet->priority = flow->key.phy.priority;
  463. packet->mark = flow->key.phy.skb_mark;
  464. rcu_read_lock();
  465. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  466. err = -ENODEV;
  467. if (!dp)
  468. goto err_unlock;
  469. local_bh_disable();
  470. err = ovs_execute_actions(dp, packet);
  471. local_bh_enable();
  472. rcu_read_unlock();
  473. ovs_flow_free(flow, false);
  474. return err;
  475. err_unlock:
  476. rcu_read_unlock();
  477. err_flow_free:
  478. ovs_flow_free(flow, false);
  479. err_kfree_skb:
  480. kfree_skb(packet);
  481. err:
  482. return err;
  483. }
  484. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  485. [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
  486. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  487. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  488. };
  489. static const struct genl_ops dp_packet_genl_ops[] = {
  490. { .cmd = OVS_PACKET_CMD_EXECUTE,
  491. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  492. .policy = packet_policy,
  493. .doit = ovs_packet_cmd_execute
  494. }
  495. };
  496. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
  497. struct ovs_dp_megaflow_stats *mega_stats)
  498. {
  499. int i;
  500. memset(mega_stats, 0, sizeof(*mega_stats));
  501. stats->n_flows = ovs_flow_tbl_count(&dp->table);
  502. mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
  503. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  504. for_each_possible_cpu(i) {
  505. const struct dp_stats_percpu *percpu_stats;
  506. struct dp_stats_percpu local_stats;
  507. unsigned int start;
  508. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  509. do {
  510. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  511. local_stats = *percpu_stats;
  512. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  513. stats->n_hit += local_stats.n_hit;
  514. stats->n_missed += local_stats.n_missed;
  515. stats->n_lost += local_stats.n_lost;
  516. mega_stats->n_mask_hit += local_stats.n_mask_hit;
  517. }
  518. }
  519. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  520. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  521. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  522. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  523. };
  524. static struct genl_family dp_flow_genl_family = {
  525. .id = GENL_ID_GENERATE,
  526. .hdrsize = sizeof(struct ovs_header),
  527. .name = OVS_FLOW_FAMILY,
  528. .version = OVS_FLOW_VERSION,
  529. .maxattr = OVS_FLOW_ATTR_MAX,
  530. .netnsok = true,
  531. .parallel_ops = true,
  532. };
  533. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  534. .name = OVS_FLOW_MCGROUP
  535. };
  536. static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
  537. {
  538. return NLMSG_ALIGN(sizeof(struct ovs_header))
  539. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
  540. + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
  541. + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
  542. + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
  543. + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
  544. + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
  545. }
  546. /* Called with ovs_mutex. */
  547. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  548. struct sk_buff *skb, u32 portid,
  549. u32 seq, u32 flags, u8 cmd)
  550. {
  551. const int skb_orig_len = skb->len;
  552. struct nlattr *start;
  553. struct ovs_flow_stats stats;
  554. __be16 tcp_flags;
  555. unsigned long used;
  556. struct ovs_header *ovs_header;
  557. struct nlattr *nla;
  558. int err;
  559. ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
  560. if (!ovs_header)
  561. return -EMSGSIZE;
  562. ovs_header->dp_ifindex = get_dpifindex(dp);
  563. /* Fill flow key. */
  564. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  565. if (!nla)
  566. goto nla_put_failure;
  567. err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
  568. if (err)
  569. goto error;
  570. nla_nest_end(skb, nla);
  571. nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
  572. if (!nla)
  573. goto nla_put_failure;
  574. err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
  575. if (err)
  576. goto error;
  577. nla_nest_end(skb, nla);
  578. ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
  579. if (used &&
  580. nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
  581. goto nla_put_failure;
  582. if (stats.n_packets &&
  583. nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
  584. goto nla_put_failure;
  585. if ((u8)ntohs(tcp_flags) &&
  586. nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
  587. goto nla_put_failure;
  588. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  589. * this is the first flow to be dumped into 'skb'. This is unusual for
  590. * Netlink but individual action lists can be longer than
  591. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  592. * The userspace caller can always fetch the actions separately if it
  593. * really wants them. (Most userspace callers in fact don't care.)
  594. *
  595. * This can only fail for dump operations because the skb is always
  596. * properly sized for single flows.
  597. */
  598. start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
  599. if (start) {
  600. const struct sw_flow_actions *sf_acts;
  601. sf_acts = rcu_dereference_ovsl(flow->sf_acts);
  602. err = ovs_nla_put_actions(sf_acts->actions,
  603. sf_acts->actions_len, skb);
  604. if (!err)
  605. nla_nest_end(skb, start);
  606. else {
  607. if (skb_orig_len)
  608. goto error;
  609. nla_nest_cancel(skb, start);
  610. }
  611. } else if (skb_orig_len)
  612. goto nla_put_failure;
  613. return genlmsg_end(skb, ovs_header);
  614. nla_put_failure:
  615. err = -EMSGSIZE;
  616. error:
  617. genlmsg_cancel(skb, ovs_header);
  618. return err;
  619. }
  620. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow,
  621. struct genl_info *info)
  622. {
  623. size_t len;
  624. len = ovs_flow_cmd_msg_size(ovsl_dereference(flow->sf_acts));
  625. return genlmsg_new_unicast(len, info, GFP_KERNEL);
  626. }
  627. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  628. struct datapath *dp,
  629. struct genl_info *info,
  630. u8 cmd)
  631. {
  632. struct sk_buff *skb;
  633. int retval;
  634. skb = ovs_flow_cmd_alloc_info(flow, info);
  635. if (!skb)
  636. return ERR_PTR(-ENOMEM);
  637. retval = ovs_flow_cmd_fill_info(flow, dp, skb, info->snd_portid,
  638. info->snd_seq, 0, cmd);
  639. BUG_ON(retval < 0);
  640. return skb;
  641. }
  642. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  643. {
  644. struct nlattr **a = info->attrs;
  645. struct ovs_header *ovs_header = info->userhdr;
  646. struct sw_flow_key key, masked_key;
  647. struct sw_flow *flow = NULL;
  648. struct sw_flow_mask mask;
  649. struct sk_buff *reply;
  650. struct datapath *dp;
  651. struct sw_flow_actions *acts = NULL;
  652. struct sw_flow_match match;
  653. bool exact_5tuple;
  654. int error;
  655. /* Extract key. */
  656. error = -EINVAL;
  657. if (!a[OVS_FLOW_ATTR_KEY])
  658. goto error;
  659. ovs_match_init(&match, &key, &mask);
  660. error = ovs_nla_get_match(&match, &exact_5tuple,
  661. a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
  662. if (error)
  663. goto error;
  664. /* Validate actions. */
  665. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  666. acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
  667. error = PTR_ERR(acts);
  668. if (IS_ERR(acts))
  669. goto error;
  670. ovs_flow_mask_key(&masked_key, &key, &mask);
  671. error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
  672. &masked_key, 0, &acts);
  673. if (error) {
  674. OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
  675. goto err_kfree;
  676. }
  677. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  678. error = -EINVAL;
  679. goto error;
  680. }
  681. ovs_lock();
  682. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  683. error = -ENODEV;
  684. if (!dp)
  685. goto err_unlock_ovs;
  686. /* Check if this is a duplicate flow */
  687. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  688. if (!flow) {
  689. /* Bail out if we're not allowed to create a new flow. */
  690. error = -ENOENT;
  691. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  692. goto err_unlock_ovs;
  693. /* Allocate flow. */
  694. flow = ovs_flow_alloc(!exact_5tuple);
  695. if (IS_ERR(flow)) {
  696. error = PTR_ERR(flow);
  697. goto err_unlock_ovs;
  698. }
  699. flow->key = masked_key;
  700. flow->unmasked_key = key;
  701. rcu_assign_pointer(flow->sf_acts, acts);
  702. /* Put flow in bucket. */
  703. error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
  704. if (error) {
  705. acts = NULL;
  706. goto err_flow_free;
  707. }
  708. reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
  709. } else {
  710. /* We found a matching flow. */
  711. struct sw_flow_actions *old_acts;
  712. /* Bail out if we're not allowed to modify an existing flow.
  713. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  714. * because Generic Netlink treats the latter as a dump
  715. * request. We also accept NLM_F_EXCL in case that bug ever
  716. * gets fixed.
  717. */
  718. error = -EEXIST;
  719. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  720. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  721. goto err_unlock_ovs;
  722. /* The unmasked key has to be the same for flow updates. */
  723. if (!ovs_flow_cmp_unmasked_key(flow, &match))
  724. goto err_unlock_ovs;
  725. /* Update actions. */
  726. old_acts = ovsl_dereference(flow->sf_acts);
  727. rcu_assign_pointer(flow->sf_acts, acts);
  728. ovs_nla_free_flow_actions(old_acts);
  729. reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
  730. /* Clear stats. */
  731. if (a[OVS_FLOW_ATTR_CLEAR])
  732. ovs_flow_stats_clear(flow);
  733. }
  734. ovs_unlock();
  735. if (!IS_ERR(reply))
  736. ovs_notify(&dp_flow_genl_family, reply, info);
  737. else
  738. genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
  739. 0, PTR_ERR(reply));
  740. return 0;
  741. err_flow_free:
  742. ovs_flow_free(flow, false);
  743. err_unlock_ovs:
  744. ovs_unlock();
  745. err_kfree:
  746. kfree(acts);
  747. error:
  748. return error;
  749. }
  750. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  751. {
  752. struct nlattr **a = info->attrs;
  753. struct ovs_header *ovs_header = info->userhdr;
  754. struct sw_flow_key key;
  755. struct sk_buff *reply;
  756. struct sw_flow *flow;
  757. struct datapath *dp;
  758. struct sw_flow_match match;
  759. int err;
  760. if (!a[OVS_FLOW_ATTR_KEY]) {
  761. OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
  762. return -EINVAL;
  763. }
  764. ovs_match_init(&match, &key, NULL);
  765. err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
  766. if (err)
  767. return err;
  768. ovs_lock();
  769. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  770. if (!dp) {
  771. err = -ENODEV;
  772. goto unlock;
  773. }
  774. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  775. if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
  776. err = -ENOENT;
  777. goto unlock;
  778. }
  779. reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
  780. if (IS_ERR(reply)) {
  781. err = PTR_ERR(reply);
  782. goto unlock;
  783. }
  784. ovs_unlock();
  785. return genlmsg_reply(reply, info);
  786. unlock:
  787. ovs_unlock();
  788. return err;
  789. }
  790. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  791. {
  792. struct nlattr **a = info->attrs;
  793. struct ovs_header *ovs_header = info->userhdr;
  794. struct sw_flow_key key;
  795. struct sk_buff *reply;
  796. struct sw_flow *flow;
  797. struct datapath *dp;
  798. struct sw_flow_match match;
  799. int err;
  800. ovs_lock();
  801. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  802. if (!dp) {
  803. err = -ENODEV;
  804. goto unlock;
  805. }
  806. if (!a[OVS_FLOW_ATTR_KEY]) {
  807. err = ovs_flow_tbl_flush(&dp->table);
  808. goto unlock;
  809. }
  810. ovs_match_init(&match, &key, NULL);
  811. err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
  812. if (err)
  813. goto unlock;
  814. flow = ovs_flow_tbl_lookup(&dp->table, &key);
  815. if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
  816. err = -ENOENT;
  817. goto unlock;
  818. }
  819. reply = ovs_flow_cmd_alloc_info(flow, info);
  820. if (!reply) {
  821. err = -ENOMEM;
  822. goto unlock;
  823. }
  824. ovs_flow_tbl_remove(&dp->table, flow);
  825. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
  826. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  827. BUG_ON(err < 0);
  828. ovs_flow_free(flow, true);
  829. ovs_unlock();
  830. ovs_notify(&dp_flow_genl_family, reply, info);
  831. return 0;
  832. unlock:
  833. ovs_unlock();
  834. return err;
  835. }
  836. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  837. {
  838. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  839. struct table_instance *ti;
  840. struct datapath *dp;
  841. rcu_read_lock();
  842. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  843. if (!dp) {
  844. rcu_read_unlock();
  845. return -ENODEV;
  846. }
  847. ti = rcu_dereference(dp->table.ti);
  848. for (;;) {
  849. struct sw_flow *flow;
  850. u32 bucket, obj;
  851. bucket = cb->args[0];
  852. obj = cb->args[1];
  853. flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
  854. if (!flow)
  855. break;
  856. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  857. NETLINK_CB(cb->skb).portid,
  858. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  859. OVS_FLOW_CMD_NEW) < 0)
  860. break;
  861. cb->args[0] = bucket;
  862. cb->args[1] = obj;
  863. }
  864. rcu_read_unlock();
  865. return skb->len;
  866. }
  867. static const struct genl_ops dp_flow_genl_ops[] = {
  868. { .cmd = OVS_FLOW_CMD_NEW,
  869. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  870. .policy = flow_policy,
  871. .doit = ovs_flow_cmd_new_or_set
  872. },
  873. { .cmd = OVS_FLOW_CMD_DEL,
  874. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  875. .policy = flow_policy,
  876. .doit = ovs_flow_cmd_del
  877. },
  878. { .cmd = OVS_FLOW_CMD_GET,
  879. .flags = 0, /* OK for unprivileged users. */
  880. .policy = flow_policy,
  881. .doit = ovs_flow_cmd_get,
  882. .dumpit = ovs_flow_cmd_dump
  883. },
  884. { .cmd = OVS_FLOW_CMD_SET,
  885. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  886. .policy = flow_policy,
  887. .doit = ovs_flow_cmd_new_or_set,
  888. },
  889. };
  890. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  891. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  892. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  893. [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
  894. };
  895. static struct genl_family dp_datapath_genl_family = {
  896. .id = GENL_ID_GENERATE,
  897. .hdrsize = sizeof(struct ovs_header),
  898. .name = OVS_DATAPATH_FAMILY,
  899. .version = OVS_DATAPATH_VERSION,
  900. .maxattr = OVS_DP_ATTR_MAX,
  901. .netnsok = true,
  902. .parallel_ops = true,
  903. };
  904. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  905. .name = OVS_DATAPATH_MCGROUP
  906. };
  907. static size_t ovs_dp_cmd_msg_size(void)
  908. {
  909. size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
  910. msgsize += nla_total_size(IFNAMSIZ);
  911. msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
  912. msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
  913. msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
  914. return msgsize;
  915. }
  916. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  917. u32 portid, u32 seq, u32 flags, u8 cmd)
  918. {
  919. struct ovs_header *ovs_header;
  920. struct ovs_dp_stats dp_stats;
  921. struct ovs_dp_megaflow_stats dp_megaflow_stats;
  922. int err;
  923. ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
  924. flags, cmd);
  925. if (!ovs_header)
  926. goto error;
  927. ovs_header->dp_ifindex = get_dpifindex(dp);
  928. rcu_read_lock();
  929. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  930. rcu_read_unlock();
  931. if (err)
  932. goto nla_put_failure;
  933. get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
  934. if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
  935. &dp_stats))
  936. goto nla_put_failure;
  937. if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
  938. sizeof(struct ovs_dp_megaflow_stats),
  939. &dp_megaflow_stats))
  940. goto nla_put_failure;
  941. if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
  942. goto nla_put_failure;
  943. return genlmsg_end(skb, ovs_header);
  944. nla_put_failure:
  945. genlmsg_cancel(skb, ovs_header);
  946. error:
  947. return -EMSGSIZE;
  948. }
  949. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp,
  950. struct genl_info *info, u8 cmd)
  951. {
  952. struct sk_buff *skb;
  953. int retval;
  954. skb = genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
  955. if (!skb)
  956. return ERR_PTR(-ENOMEM);
  957. retval = ovs_dp_cmd_fill_info(dp, skb, info->snd_portid, info->snd_seq, 0, cmd);
  958. if (retval < 0) {
  959. kfree_skb(skb);
  960. return ERR_PTR(retval);
  961. }
  962. return skb;
  963. }
  964. /* Called with ovs_mutex. */
  965. static struct datapath *lookup_datapath(struct net *net,
  966. struct ovs_header *ovs_header,
  967. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  968. {
  969. struct datapath *dp;
  970. if (!a[OVS_DP_ATTR_NAME])
  971. dp = get_dp(net, ovs_header->dp_ifindex);
  972. else {
  973. struct vport *vport;
  974. rcu_read_lock();
  975. vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
  976. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  977. rcu_read_unlock();
  978. }
  979. return dp ? dp : ERR_PTR(-ENODEV);
  980. }
  981. static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
  982. {
  983. struct datapath *dp;
  984. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  985. if (!dp)
  986. return;
  987. WARN(dp->user_features, "Dropping previously announced user features\n");
  988. dp->user_features = 0;
  989. }
  990. static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
  991. {
  992. if (a[OVS_DP_ATTR_USER_FEATURES])
  993. dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
  994. }
  995. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  996. {
  997. struct nlattr **a = info->attrs;
  998. struct vport_parms parms;
  999. struct sk_buff *reply;
  1000. struct datapath *dp;
  1001. struct vport *vport;
  1002. struct ovs_net *ovs_net;
  1003. int err, i;
  1004. err = -EINVAL;
  1005. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1006. goto err;
  1007. ovs_lock();
  1008. err = -ENOMEM;
  1009. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1010. if (dp == NULL)
  1011. goto err_unlock_ovs;
  1012. ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
  1013. /* Allocate table. */
  1014. err = ovs_flow_tbl_init(&dp->table);
  1015. if (err)
  1016. goto err_free_dp;
  1017. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1018. if (!dp->stats_percpu) {
  1019. err = -ENOMEM;
  1020. goto err_destroy_table;
  1021. }
  1022. for_each_possible_cpu(i) {
  1023. struct dp_stats_percpu *dpath_stats;
  1024. dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
  1025. u64_stats_init(&dpath_stats->sync);
  1026. }
  1027. dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
  1028. GFP_KERNEL);
  1029. if (!dp->ports) {
  1030. err = -ENOMEM;
  1031. goto err_destroy_percpu;
  1032. }
  1033. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
  1034. INIT_HLIST_HEAD(&dp->ports[i]);
  1035. /* Set up our datapath device. */
  1036. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1037. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1038. parms.options = NULL;
  1039. parms.dp = dp;
  1040. parms.port_no = OVSP_LOCAL;
  1041. parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1042. ovs_dp_change(dp, a);
  1043. vport = new_vport(&parms);
  1044. if (IS_ERR(vport)) {
  1045. err = PTR_ERR(vport);
  1046. if (err == -EBUSY)
  1047. err = -EEXIST;
  1048. if (err == -EEXIST) {
  1049. /* An outdated user space instance that does not understand
  1050. * the concept of user_features has attempted to create a new
  1051. * datapath and is likely to reuse it. Drop all user features.
  1052. */
  1053. if (info->genlhdr->version < OVS_DP_VER_FEATURES)
  1054. ovs_dp_reset_user_features(skb, info);
  1055. }
  1056. goto err_destroy_ports_array;
  1057. }
  1058. reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
  1059. err = PTR_ERR(reply);
  1060. if (IS_ERR(reply))
  1061. goto err_destroy_local_port;
  1062. ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
  1063. list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
  1064. ovs_unlock();
  1065. ovs_notify(&dp_datapath_genl_family, reply, info);
  1066. return 0;
  1067. err_destroy_local_port:
  1068. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1069. err_destroy_ports_array:
  1070. kfree(dp->ports);
  1071. err_destroy_percpu:
  1072. free_percpu(dp->stats_percpu);
  1073. err_destroy_table:
  1074. ovs_flow_tbl_destroy(&dp->table, false);
  1075. err_free_dp:
  1076. release_net(ovs_dp_get_net(dp));
  1077. kfree(dp);
  1078. err_unlock_ovs:
  1079. ovs_unlock();
  1080. err:
  1081. return err;
  1082. }
  1083. /* Called with ovs_mutex. */
  1084. static void __dp_destroy(struct datapath *dp)
  1085. {
  1086. int i;
  1087. for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
  1088. struct vport *vport;
  1089. struct hlist_node *n;
  1090. hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
  1091. if (vport->port_no != OVSP_LOCAL)
  1092. ovs_dp_detach_port(vport);
  1093. }
  1094. list_del_rcu(&dp->list_node);
  1095. /* OVSP_LOCAL is datapath internal port. We need to make sure that
  1096. * all ports in datapath are destroyed first before freeing datapath.
  1097. */
  1098. ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
  1099. /* RCU destroy the flow table */
  1100. ovs_flow_tbl_destroy(&dp->table, true);
  1101. call_rcu(&dp->rcu, destroy_dp_rcu);
  1102. }
  1103. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1104. {
  1105. struct sk_buff *reply;
  1106. struct datapath *dp;
  1107. int err;
  1108. ovs_lock();
  1109. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1110. err = PTR_ERR(dp);
  1111. if (IS_ERR(dp))
  1112. goto unlock;
  1113. reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_DEL);
  1114. err = PTR_ERR(reply);
  1115. if (IS_ERR(reply))
  1116. goto unlock;
  1117. __dp_destroy(dp);
  1118. ovs_unlock();
  1119. ovs_notify(&dp_datapath_genl_family, reply, info);
  1120. return 0;
  1121. unlock:
  1122. ovs_unlock();
  1123. return err;
  1124. }
  1125. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1126. {
  1127. struct sk_buff *reply;
  1128. struct datapath *dp;
  1129. int err;
  1130. ovs_lock();
  1131. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1132. err = PTR_ERR(dp);
  1133. if (IS_ERR(dp))
  1134. goto unlock;
  1135. ovs_dp_change(dp, info->attrs);
  1136. reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
  1137. if (IS_ERR(reply)) {
  1138. err = PTR_ERR(reply);
  1139. genl_set_err(&dp_datapath_genl_family, sock_net(skb->sk), 0,
  1140. 0, err);
  1141. err = 0;
  1142. goto unlock;
  1143. }
  1144. ovs_unlock();
  1145. ovs_notify(&dp_datapath_genl_family, reply, info);
  1146. return 0;
  1147. unlock:
  1148. ovs_unlock();
  1149. return err;
  1150. }
  1151. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1152. {
  1153. struct sk_buff *reply;
  1154. struct datapath *dp;
  1155. int err;
  1156. ovs_lock();
  1157. dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
  1158. if (IS_ERR(dp)) {
  1159. err = PTR_ERR(dp);
  1160. goto unlock;
  1161. }
  1162. reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
  1163. if (IS_ERR(reply)) {
  1164. err = PTR_ERR(reply);
  1165. goto unlock;
  1166. }
  1167. ovs_unlock();
  1168. return genlmsg_reply(reply, info);
  1169. unlock:
  1170. ovs_unlock();
  1171. return err;
  1172. }
  1173. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1174. {
  1175. struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
  1176. struct datapath *dp;
  1177. int skip = cb->args[0];
  1178. int i = 0;
  1179. rcu_read_lock();
  1180. list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
  1181. if (i >= skip &&
  1182. ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
  1183. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1184. OVS_DP_CMD_NEW) < 0)
  1185. break;
  1186. i++;
  1187. }
  1188. rcu_read_unlock();
  1189. cb->args[0] = i;
  1190. return skb->len;
  1191. }
  1192. static const struct genl_ops dp_datapath_genl_ops[] = {
  1193. { .cmd = OVS_DP_CMD_NEW,
  1194. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1195. .policy = datapath_policy,
  1196. .doit = ovs_dp_cmd_new
  1197. },
  1198. { .cmd = OVS_DP_CMD_DEL,
  1199. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1200. .policy = datapath_policy,
  1201. .doit = ovs_dp_cmd_del
  1202. },
  1203. { .cmd = OVS_DP_CMD_GET,
  1204. .flags = 0, /* OK for unprivileged users. */
  1205. .policy = datapath_policy,
  1206. .doit = ovs_dp_cmd_get,
  1207. .dumpit = ovs_dp_cmd_dump
  1208. },
  1209. { .cmd = OVS_DP_CMD_SET,
  1210. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1211. .policy = datapath_policy,
  1212. .doit = ovs_dp_cmd_set,
  1213. },
  1214. };
  1215. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1216. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1217. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1218. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1219. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1220. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1221. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1222. };
  1223. struct genl_family dp_vport_genl_family = {
  1224. .id = GENL_ID_GENERATE,
  1225. .hdrsize = sizeof(struct ovs_header),
  1226. .name = OVS_VPORT_FAMILY,
  1227. .version = OVS_VPORT_VERSION,
  1228. .maxattr = OVS_VPORT_ATTR_MAX,
  1229. .netnsok = true,
  1230. .parallel_ops = true,
  1231. };
  1232. static struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1233. .name = OVS_VPORT_MCGROUP
  1234. };
  1235. /* Called with ovs_mutex or RCU read lock. */
  1236. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1237. u32 portid, u32 seq, u32 flags, u8 cmd)
  1238. {
  1239. struct ovs_header *ovs_header;
  1240. struct ovs_vport_stats vport_stats;
  1241. int err;
  1242. ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
  1243. flags, cmd);
  1244. if (!ovs_header)
  1245. return -EMSGSIZE;
  1246. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1247. if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
  1248. nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
  1249. nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
  1250. nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
  1251. goto nla_put_failure;
  1252. ovs_vport_get_stats(vport, &vport_stats);
  1253. if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1254. &vport_stats))
  1255. goto nla_put_failure;
  1256. err = ovs_vport_get_options(vport, skb);
  1257. if (err == -EMSGSIZE)
  1258. goto error;
  1259. return genlmsg_end(skb, ovs_header);
  1260. nla_put_failure:
  1261. err = -EMSGSIZE;
  1262. error:
  1263. genlmsg_cancel(skb, ovs_header);
  1264. return err;
  1265. }
  1266. /* Called with ovs_mutex or RCU read lock. */
  1267. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
  1268. u32 seq, u8 cmd)
  1269. {
  1270. struct sk_buff *skb;
  1271. int retval;
  1272. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1273. if (!skb)
  1274. return ERR_PTR(-ENOMEM);
  1275. retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
  1276. BUG_ON(retval < 0);
  1277. return skb;
  1278. }
  1279. /* Called with ovs_mutex or RCU read lock. */
  1280. static struct vport *lookup_vport(struct net *net,
  1281. struct ovs_header *ovs_header,
  1282. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1283. {
  1284. struct datapath *dp;
  1285. struct vport *vport;
  1286. if (a[OVS_VPORT_ATTR_NAME]) {
  1287. vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
  1288. if (!vport)
  1289. return ERR_PTR(-ENODEV);
  1290. if (ovs_header->dp_ifindex &&
  1291. ovs_header->dp_ifindex != get_dpifindex(vport->dp))
  1292. return ERR_PTR(-ENODEV);
  1293. return vport;
  1294. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1295. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1296. if (port_no >= DP_MAX_PORTS)
  1297. return ERR_PTR(-EFBIG);
  1298. dp = get_dp(net, ovs_header->dp_ifindex);
  1299. if (!dp)
  1300. return ERR_PTR(-ENODEV);
  1301. vport = ovs_vport_ovsl_rcu(dp, port_no);
  1302. if (!vport)
  1303. return ERR_PTR(-ENODEV);
  1304. return vport;
  1305. } else
  1306. return ERR_PTR(-EINVAL);
  1307. }
  1308. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1309. {
  1310. struct nlattr **a = info->attrs;
  1311. struct ovs_header *ovs_header = info->userhdr;
  1312. struct vport_parms parms;
  1313. struct sk_buff *reply;
  1314. struct vport *vport;
  1315. struct datapath *dp;
  1316. u32 port_no;
  1317. int err;
  1318. err = -EINVAL;
  1319. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1320. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1321. goto exit;
  1322. ovs_lock();
  1323. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1324. err = -ENODEV;
  1325. if (!dp)
  1326. goto exit_unlock;
  1327. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1328. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1329. err = -EFBIG;
  1330. if (port_no >= DP_MAX_PORTS)
  1331. goto exit_unlock;
  1332. vport = ovs_vport_ovsl(dp, port_no);
  1333. err = -EBUSY;
  1334. if (vport)
  1335. goto exit_unlock;
  1336. } else {
  1337. for (port_no = 1; ; port_no++) {
  1338. if (port_no >= DP_MAX_PORTS) {
  1339. err = -EFBIG;
  1340. goto exit_unlock;
  1341. }
  1342. vport = ovs_vport_ovsl(dp, port_no);
  1343. if (!vport)
  1344. break;
  1345. }
  1346. }
  1347. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1348. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1349. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1350. parms.dp = dp;
  1351. parms.port_no = port_no;
  1352. parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1353. vport = new_vport(&parms);
  1354. err = PTR_ERR(vport);
  1355. if (IS_ERR(vport))
  1356. goto exit_unlock;
  1357. err = 0;
  1358. reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
  1359. OVS_VPORT_CMD_NEW);
  1360. if (IS_ERR(reply)) {
  1361. err = PTR_ERR(reply);
  1362. ovs_dp_detach_port(vport);
  1363. goto exit_unlock;
  1364. }
  1365. ovs_notify(&dp_vport_genl_family, reply, info);
  1366. exit_unlock:
  1367. ovs_unlock();
  1368. exit:
  1369. return err;
  1370. }
  1371. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1372. {
  1373. struct nlattr **a = info->attrs;
  1374. struct sk_buff *reply;
  1375. struct vport *vport;
  1376. int err;
  1377. ovs_lock();
  1378. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1379. err = PTR_ERR(vport);
  1380. if (IS_ERR(vport))
  1381. goto exit_unlock;
  1382. if (a[OVS_VPORT_ATTR_TYPE] &&
  1383. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
  1384. err = -EINVAL;
  1385. goto exit_unlock;
  1386. }
  1387. reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1388. if (!reply) {
  1389. err = -ENOMEM;
  1390. goto exit_unlock;
  1391. }
  1392. if (a[OVS_VPORT_ATTR_OPTIONS]) {
  1393. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1394. if (err)
  1395. goto exit_free;
  1396. }
  1397. if (a[OVS_VPORT_ATTR_UPCALL_PID])
  1398. vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1399. err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
  1400. info->snd_seq, 0, OVS_VPORT_CMD_NEW);
  1401. BUG_ON(err < 0);
  1402. ovs_unlock();
  1403. ovs_notify(&dp_vport_genl_family, reply, info);
  1404. return 0;
  1405. exit_free:
  1406. kfree_skb(reply);
  1407. exit_unlock:
  1408. ovs_unlock();
  1409. return err;
  1410. }
  1411. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1412. {
  1413. struct nlattr **a = info->attrs;
  1414. struct sk_buff *reply;
  1415. struct vport *vport;
  1416. int err;
  1417. ovs_lock();
  1418. vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
  1419. err = PTR_ERR(vport);
  1420. if (IS_ERR(vport))
  1421. goto exit_unlock;
  1422. if (vport->port_no == OVSP_LOCAL) {
  1423. err = -EINVAL;
  1424. goto exit_unlock;
  1425. }
  1426. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1427. info->snd_seq, OVS_VPORT_CMD_DEL);
  1428. err = PTR_ERR(reply);
  1429. if (IS_ERR(reply))
  1430. goto exit_unlock;
  1431. err = 0;
  1432. ovs_dp_detach_port(vport);
  1433. ovs_notify(&dp_vport_genl_family, reply, info);
  1434. exit_unlock:
  1435. ovs_unlock();
  1436. return err;
  1437. }
  1438. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1439. {
  1440. struct nlattr **a = info->attrs;
  1441. struct ovs_header *ovs_header = info->userhdr;
  1442. struct sk_buff *reply;
  1443. struct vport *vport;
  1444. int err;
  1445. rcu_read_lock();
  1446. vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
  1447. err = PTR_ERR(vport);
  1448. if (IS_ERR(vport))
  1449. goto exit_unlock;
  1450. reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
  1451. info->snd_seq, OVS_VPORT_CMD_NEW);
  1452. err = PTR_ERR(reply);
  1453. if (IS_ERR(reply))
  1454. goto exit_unlock;
  1455. rcu_read_unlock();
  1456. return genlmsg_reply(reply, info);
  1457. exit_unlock:
  1458. rcu_read_unlock();
  1459. return err;
  1460. }
  1461. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1462. {
  1463. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1464. struct datapath *dp;
  1465. int bucket = cb->args[0], skip = cb->args[1];
  1466. int i, j = 0;
  1467. dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
  1468. if (!dp)
  1469. return -ENODEV;
  1470. rcu_read_lock();
  1471. for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
  1472. struct vport *vport;
  1473. j = 0;
  1474. hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
  1475. if (j >= skip &&
  1476. ovs_vport_cmd_fill_info(vport, skb,
  1477. NETLINK_CB(cb->skb).portid,
  1478. cb->nlh->nlmsg_seq,
  1479. NLM_F_MULTI,
  1480. OVS_VPORT_CMD_NEW) < 0)
  1481. goto out;
  1482. j++;
  1483. }
  1484. skip = 0;
  1485. }
  1486. out:
  1487. rcu_read_unlock();
  1488. cb->args[0] = i;
  1489. cb->args[1] = j;
  1490. return skb->len;
  1491. }
  1492. static const struct genl_ops dp_vport_genl_ops[] = {
  1493. { .cmd = OVS_VPORT_CMD_NEW,
  1494. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1495. .policy = vport_policy,
  1496. .doit = ovs_vport_cmd_new
  1497. },
  1498. { .cmd = OVS_VPORT_CMD_DEL,
  1499. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1500. .policy = vport_policy,
  1501. .doit = ovs_vport_cmd_del
  1502. },
  1503. { .cmd = OVS_VPORT_CMD_GET,
  1504. .flags = 0, /* OK for unprivileged users. */
  1505. .policy = vport_policy,
  1506. .doit = ovs_vport_cmd_get,
  1507. .dumpit = ovs_vport_cmd_dump
  1508. },
  1509. { .cmd = OVS_VPORT_CMD_SET,
  1510. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1511. .policy = vport_policy,
  1512. .doit = ovs_vport_cmd_set,
  1513. },
  1514. };
  1515. struct genl_family_and_ops {
  1516. struct genl_family *family;
  1517. const struct genl_ops *ops;
  1518. int n_ops;
  1519. const struct genl_multicast_group *group;
  1520. };
  1521. static const struct genl_family_and_ops dp_genl_families[] = {
  1522. { &dp_datapath_genl_family,
  1523. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1524. &ovs_dp_datapath_multicast_group },
  1525. { &dp_vport_genl_family,
  1526. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1527. &ovs_dp_vport_multicast_group },
  1528. { &dp_flow_genl_family,
  1529. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1530. &ovs_dp_flow_multicast_group },
  1531. { &dp_packet_genl_family,
  1532. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1533. NULL },
  1534. };
  1535. static void dp_unregister_genl(int n_families)
  1536. {
  1537. int i;
  1538. for (i = 0; i < n_families; i++)
  1539. genl_unregister_family(dp_genl_families[i].family);
  1540. }
  1541. static int dp_register_genl(void)
  1542. {
  1543. int n_registered;
  1544. int err;
  1545. int i;
  1546. n_registered = 0;
  1547. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1548. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1549. f->family->ops = f->ops;
  1550. f->family->n_ops = f->n_ops;
  1551. f->family->mcgrps = f->group;
  1552. f->family->n_mcgrps = f->group ? 1 : 0;
  1553. err = genl_register_family(f->family);
  1554. if (err)
  1555. goto error;
  1556. n_registered++;
  1557. }
  1558. return 0;
  1559. error:
  1560. dp_unregister_genl(n_registered);
  1561. return err;
  1562. }
  1563. static int __net_init ovs_init_net(struct net *net)
  1564. {
  1565. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1566. INIT_LIST_HEAD(&ovs_net->dps);
  1567. INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
  1568. return 0;
  1569. }
  1570. static void __net_exit ovs_exit_net(struct net *net)
  1571. {
  1572. struct datapath *dp, *dp_next;
  1573. struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
  1574. ovs_lock();
  1575. list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
  1576. __dp_destroy(dp);
  1577. ovs_unlock();
  1578. cancel_work_sync(&ovs_net->dp_notify_work);
  1579. }
  1580. static struct pernet_operations ovs_net_ops = {
  1581. .init = ovs_init_net,
  1582. .exit = ovs_exit_net,
  1583. .id = &ovs_net_id,
  1584. .size = sizeof(struct ovs_net),
  1585. };
  1586. static int __init dp_init(void)
  1587. {
  1588. int err;
  1589. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
  1590. pr_info("Open vSwitch switching datapath\n");
  1591. err = ovs_flow_init();
  1592. if (err)
  1593. goto error;
  1594. err = ovs_vport_init();
  1595. if (err)
  1596. goto error_flow_exit;
  1597. err = register_pernet_device(&ovs_net_ops);
  1598. if (err)
  1599. goto error_vport_exit;
  1600. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1601. if (err)
  1602. goto error_netns_exit;
  1603. err = dp_register_genl();
  1604. if (err < 0)
  1605. goto error_unreg_notifier;
  1606. return 0;
  1607. error_unreg_notifier:
  1608. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1609. error_netns_exit:
  1610. unregister_pernet_device(&ovs_net_ops);
  1611. error_vport_exit:
  1612. ovs_vport_exit();
  1613. error_flow_exit:
  1614. ovs_flow_exit();
  1615. error:
  1616. return err;
  1617. }
  1618. static void dp_cleanup(void)
  1619. {
  1620. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1621. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1622. unregister_pernet_device(&ovs_net_ops);
  1623. rcu_barrier();
  1624. ovs_vport_exit();
  1625. ovs_flow_exit();
  1626. }
  1627. module_init(dp_init);
  1628. module_exit(dp_cleanup);
  1629. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1630. MODULE_LICENSE("GPL");