datapath.c 53 KB

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